没有看到播种数据库后所需的续集模型属性

更新


下面有人建议添加一个模型 ID 来进行喊叫,我不再收到错误,但现在没有任何内容被保存到我的数据库中?添加以下新信息:


我在用户和喊话之间有一对多的关系。两种模型都有电子邮件属性,我正在尝试使用一种神奇的方法来设置喊话。当我使用 user.createShoutout() 时,我可以生成喊话,但电子邮件属性不会显示在数据库中。


const Sequelize = require('sequelize')

const db = require('../db')


const Shoutout = db.define('shoutout', {

//NEW

  id: {

    allowNull: false,

    autoIncrement: true,

    primaryKey: true,

    type: Sequelize.INTEGER

  }, //OLD

  name: {

    type: Sequelize.STRING,

    validate: {

      notEmpty: true

    },

    email: {

      type: Sequelize.STRING,

      unique: true,

      allowNull: false

    }

  },

  message: {

    type: Sequelize.TEXT,

    allowNull: false

  },

  from: {

    type: Sequelize.STRING

  }

})


module.exports = Shoutout

协会:


User.hasMany(Shoutouts)

Shoutouts.belongsTo(User)


User.hasMany(Emails)

Emails.belongsTo(User)

当我使用 user.AddShoutout() 时,如下所示:


   let paramsObj = {     

        name: addEmail.firstName,

        email:addEmail.email,

        message: 'test msg',

        userId: 3

}

//NEW

let id = 1;

    const addInfo = await userThree.addShoutout(id,paramsObj)

//新的


不再出现对象错误,实际上没有看到任何错误。但当我查看我的喊出表时,没有任何内容被添加。


当我 console.log addInfo 尝试创建喊话的用户被返回时?


我需要帮助来尝试让这个用户模型魔术方法生成新的喊话!


感谢您阅读本文以及任何建议!


守着一只汪
浏览 72回答 1
1回答

跃然一笑

您的电子邮件字段嵌套在姓名字段中const Sequelize = require('sequelize')const db = require('../db')const Shoutout = db.define('shoutout', {&nbsp; id: {&nbsp; &nbsp; allowNull: false,&nbsp; &nbsp; autoIncrement: true,&nbsp; &nbsp; primaryKey: true,&nbsp; &nbsp; type: Sequelize.INTEGER&nbsp; }, //OLD&nbsp; name: {&nbsp; &nbsp; type: Sequelize.STRING,&nbsp; &nbsp; validate: {&nbsp; &nbsp; &nbsp; notEmpty: true&nbsp; &nbsp; },&nbsp; &nbsp; email: { # <----------------------------- nested too deep&nbsp; &nbsp; &nbsp; type: Sequelize.STRING,&nbsp; &nbsp; &nbsp; unique: true,&nbsp; &nbsp; &nbsp; allowNull: false&nbsp; &nbsp; }&nbsp; },&nbsp; message: {&nbsp; &nbsp; type: Sequelize.TEXT,&nbsp; &nbsp; allowNull: false&nbsp; },&nbsp; from: {&nbsp; &nbsp; type: Sequelize.STRING&nbsp; }})module.exports = Shoutout
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript