background:
const EventUser = sequelize.define("userEvents", {
payStatus: Sequelize.STRING
})
User.belongsToMany(Event, { through: EventUser})
Event.belongsToMany(User, { through: EventUser})
use:
const event = await EventModel.getEventDetail(eventId);
const user = await UserModel.findUserById(userId);
//eventuser
try {
await event.addUser(user, { through: { payStatus: payStatus }}) //
} catch (e) {
console.log(e)
}
error log is as follows:
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)
ask the sequelize boss to solve the problem