更新下面有人建议添加一个模型 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}//NEWlet id = 1; const addInfo = await userThree.addShoutout(id,paramsObj)//新的不再出现对象错误,实际上没有看到任何错误。但当我查看我的喊出表时,没有任何内容被添加。当我 console.log addInfo 尝试创建喊话的用户被返回时?我需要帮助来尝试让这个用户模型魔术方法生成新的喊话!感谢您阅读本文以及任何建议!
1 回答

跃然一笑
TA贡献1826条经验 获得超6个赞
您的电子邮件字段嵌套在姓名字段中
const Sequelize = require('sequelize')
const db = require('../db')
const Shoutout = db.define('shoutout', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
}, //OLD
name: {
type: Sequelize.STRING,
validate: {
notEmpty: true
},
email: { # <----------------------------- nested too deep
type: Sequelize.STRING,
unique: true,
allowNull: false
}
},
message: {
type: Sequelize.TEXT,
allowNull: false
},
from: {
type: Sequelize.STRING
}
})
module.exports = Shoutout
添加回答
举报
0/150
提交
取消