我的bulkCreate方法存在一个问题,即我的数组中的值没有传递给bulkCreate方法,尽管它是以正确的(或我认为的)JSON格式发送的。可能是存储的变量正在添加''围绕物体?
以下是数组和对象设置代码:
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: dsfdf@st.com, organizationId: 1},{ userId: 127, memberEmail: sdfsdf@4iof.com, 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: dsfdf@st.com, organizationId: 1}',
'{ userId: 127, memberEmail: sdfsdf@4iof.com, 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')
});
答案 0 :(得分:1)
原因是当您执行memberAssociation.push("...")
时,您会在每次迭代时将String
添加到memberAssociation
数组而不是对象。你应该每次推送物品
memberAssociation.push({
userId: user[key].userId,
memberEmail: user[key].email,
organizationId: user[key].organizationId
});
然后您可以使用上面创建的数组执行bulkCreate
,就像在代码中一样。 documentation
表示records
的{{1}}参数是一个对象数组:
从
创建实例的对象列表(键/值对)