从这篇文章:Node.js / Sequelize.js / Express.js - How to insert into many-to-many association? (sync/async?)
答案仅在您创建个人和电子邮件时显示,但是,我想创建一个已创建电子邮件的个人。
正确创建个人和电子邮件的原始答案:
models.Individual.create({
name: "Test"
}).then(function(createdIndividual) { // note the argument
models.Email.create({
address: "test@gmail.com"
}).then(function(createdEmail) { // note the argument
createdIndividual.addEmail(createdEmail)
.then(function(addedEmail) { // note th-- well you get the idea :)
console.log("Success");
});
})
});
要使用已创建的电子邮件创建个人,我将其修改为:
models.Individual.create({
name: "Test"
}).then(function(createdIndividual) { // note the argument
//This email id exists in the Email table.
var email = {
id: 1
}
createdIndividual.addEmail(email)
.then(function(addedEmail) {
console.log("Success");
});
});
然后我收到了这个错误:
Unhandled rejection TypeError: val.replace is not a function
at Object.SqlString.escape (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/sql-string.js:63:15)
at Object.QueryGenerator.escape (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:977:22)
at /Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2203:23
at Array.map (native)
at Object.QueryGenerator.whereItemQuery (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2202:49)
at /Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1903:25
at /Users/Roller/Working/Web/ponds_web/node_modules/sequelize/node_modules/lodash/lodash.js:4389:15
at baseForOwn (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/node_modules/lodash/lodash.js:2652:24)
at Function.forOwn (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/node_modules/lodash/lodash.js:12254:24)
at Object.QueryGenerator.whereItemsQuery (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1902:9)
at Object.QueryGenerator.getWhereConditions (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2337:19)
at Object.QueryGenerator.selectQuery (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1451:28)
at QueryInterface.select (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/query-interface.js:669:25)
at .<anonymous> (/Users/Roller/Working/Web/ponds_web/node_modules/sequelize/lib/model.js:1390:32)
at tryCatcher (/Users/Roller/Working/Web/ponds_web/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/Roller/Working/Web/ponds_web/node_modules/bluebird/js/release/promise.js:504:31)
我认为这个错误是因为我的电子邮件对象不是Sequelize对象。
记住问题:
我们是否应该通过电子邮件模型查询将电子邮件对象转换为Sequalize对象?它会起作用吗?
将该对象转换为Sequalize的更好方法是什么?干净的代码和性能是什么?
如果在创建新产品时有多封电子邮件要添加到Individual_Email中,该怎么办?喜欢超过1个电子邮件ID。
请帮助建议。谢谢。
答案 0 :(得分:0)
你是对的,问题是电子邮件不是一个续集对象。如果在添加之前从数据库中检索它,它将起作用。另一方面,sequelize允许您通过id设置子项。所以你可以这样做:
models.Individual.create({
name: "Test"
}).then(function(createdIndividual) {
createdIndividual.setEmails([emailIds]) //emails id array
.then(function() {
console.log("Success");
});
});