0
我的bulkCreate方法存在一個問題,我的數組中的值沒有傳遞給bulkCreate方法,儘管發送的是正確的(或者我相信它是)JSON格式。難道這是事實上,存儲的變量是添加周圍的對象?Sequelize - 可接受的bulkCreate格式
這裏是陣列和對象設定碼:
return models.User.findAll({
where: {
email: { $or: userEmails}
}
}).then(function(user){
console.log('Member is triggered');
console.log(user);
for(var key in user){
memberAssociation.push("{ userId: " + user[key].userId + ", memberEmail: " + user[key].email + ", organizationId: " + user[key].organizationId + "}");
}
console.log('Here is the array ' + memberAssociation);
})
輸出console.log
:
Here is the array { userId: 126, memberEmail: [email protected], organizationId: 1},{ userId: 127, memberEmail: [email protected], organizationId: 1}
然後發送作爲bulkCreate:
.then(function(member){
console.log(memberAssociation);
return models.Member.bulkCreate(memberAssociation).then(function(member){
console.log(member);
console.log('New member')
res.send('Success')
});
輸出(成員):
[ '{ userId: 126, memberEmail: [email protected], organizationId: 1}',
'{ userId: 127, memberEmail: [email protected], organizationId: 1}' ]
SQL:
Executing (default): INSERT INTO `member` (`member_id`,`created_at`,`updated_at`) VALUES (NULL,'2017-03-06 07:36:57','2017-03-06 07:36:57'),(NULL,'2017-03-06 07:36:57','2017-03-06 07:36:57');
這裏是我完整的POST路線:
return models.User.findAll({
where: {
email: { $or: userEmails}
}
}).then(function(user){
console.log('Member is triggered');
console.log(user);
for(var key in user){
memberAssociation.push("{ userId: " + user[key].userId + ", memberEmail: " + user[key].email + ", organizationId: " + user[key].organizationId + "}");
}
console.log('Here is the array ' + memberAssociation);
}).then(function(member){
console.log(memberAssociation);
return models.Member.bulkCreate(memberAssociation).then(function(member){
console.log(member);
console.log('New member')
res.send('Success')
});
感謝創建實例追趕我的明顯缺陷。完美工作。 – cphill