sequelize建立多对多关联之后插入数据报错

发布于 2022-09-11 17:24:53 字数 5066 浏览 20 评论 0

背景:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2022-09-18 17:24:53
Event.findOne({
            where: {
                id: eventId
            },
        }).then(event => {
            console.log(event)
            User.findById(userId).then(user => {
                console.log(user)
                event.addUser(user, { through: { payStatus: payStatus }})
            });
        })

用promise写法就可以。。。await就不行?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文