sequelize建立多对多关联之后插入数据报错
背景:
const EventUser = sequelize.define('userEvents', {
payStatus: Sequelize.STRING
})
User.belongsToMany(Event, { through: EventUser})
Event.belongsToMany(User, { through: EventUser})
使用:
const event = await EventModel.getEventDetail(eventId);
const user = await UserModel.findUserById(userId);
//这里event和user都是有值的,确认
try {
await event.addUser(user, { through: { payStatus: payStatus }}) //这里报错
} catch (e) {
console.log(e)
}
报错日志如下:
Error: Invalid value user {
dataValues:
{ id: 1,
nickname: null,
session_key: 'test',
phone: null,
email: null,
money: null,
city: null,
avatarUrl: null,
sex: null,
birthday: null,
lastLoginTime: 2019-01-25T06:26:27.000Z,
registerTime: 2019-01-25T06:26:27.000Z,
openId: 'test',
createdAt: 2019-01-25T06:26:27.000Z,
updatedAt: 2019-01-25T06:26:27.000Z },
_previousDataValues:
{ id: 1,
nickname: null,
session_key: 'test',
phone: null,
email: null,
money: null,
city: null,
avatarUrl: null,
sex: null,
birthday: null,
lastLoginTime: 2019-01-25T06:26:27.000Z,
registerTime: 2019-01-25T06:26:27.000Z,
openId: 'test',
createdAt: 2019-01-25T06:26:27.000Z,
updatedAt: 2019-01-25T06:26:27.000Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: 1 },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: { plural: 'users', singular: 'user' },
omitNull: false,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [Object],
queryInterface: [Object],
models: [Object],
modelManager: [Object],
connectionManager: [Object],
importCache: [Object],
test: [Object] },
hooks: {},
uniqueKeys: { user_openId_unique: [Object] } },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes:
[ 'id',
'nickname',
'session_key',
'phone',
'email',
'money',
'city',
'avatarUrl',
'sex',
'birthday',
'lastLoginTime',
'registerTime',
'openId',
'createdAt',
'updatedAt' ] },
__eagerlyLoadedAssociations: [],
isNewRecord: false }
at Object.escape (D:\project\koa2-community\node_modules\sequelize\lib\sql-string.js:66:11)
at Object.escape (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:934:22)
at _joinKeyValue.value.map.item (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2369:69)
at Array.map (<anonymous>)
at Object._whereParseSingleValueObject (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2369:52)
at Object.whereItemQuery (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2127:19)
at Utils.getComplexKeys.forEach.prop (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1994:25)
at Array.forEach (<anonymous>)
at Object.whereItemsQuery (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1992:35)
at Object.getWhereConditions (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2456:19)
at Object.selectQuery (D:\project\koa2-community\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1140:28)
at QueryInterface.select (D:\project\koa2-community\node_modules\sequelize\lib\query-interface.js:1105:27)
at Promise.try.then.then.then (D:\project\koa2-community\node_modules\sequelize\lib\model.js:1604:34)
at tryCatcher (D:\project\koa2-community\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (D:\project\koa2-community\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (D:\project\koa2-community\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (D:\project\koa2-community\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (D:\project\koa2-community\node_modules\bluebird\js\release\promise.js:693:18)
at Async._drainQueue (D:\project\koa2-community\node_modules\bluebird\js\release\async.js:133:16)
at Async._drainQueues (D:\project\koa2-community\node_modules\bluebird\js\release\async.js:143:10)
at Immediate.Async.drainQueues (D:\project\koa2-community\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:810:20)
求sequelize大佬来解决下问题呀
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用promise写法就可以。。。await就不行?