我试图将一个带有相关对象的对象添加到续集数据库中,但我无法弄清楚如何正确地执行此操作。我已经阅读了文档,但我似乎没有在他们正在使用的对象上使用这些方法。
我的模型看起来像这样:
Client = db.define('Client', {
name: Seq.STRING,
street1: Seq.STRING,
street2: Seq.STRING,
zip: Seq.STRING(7),
city: Seq.STRING,
short: { type: Seq.STRING(10), unique: true, allowNull: true }
});
TrackedTime = db.define('TrackedTime', {
start: Seq.DATE,
end: Seq.DATE,
title: Seq.STRING,
description: Seq.TEXT
});
Client.hasMany(TrackedTime);
Client.sync();
TrackedTime.sync();
db.sync();
首先,我使用从命令行args读取的参数搜索客户端(这有效):
Clients.findAll({ where: { name: arg }}).then(function (clients) {
startTimeTrack(clients, argv.t, argv.d, start);
});
最后,当我阅读文档时,我所预期的方法无效:
function startTimeTrack(client, title, description, start) {
if (typeof client === 'undefined' || client === null)
throw "Please specify a client to track the time for!";
if (typeof title === 'undefined' || title === null)
throw "You need to specify a title!";
description = typeof description !== 'undefined' ? description : null;
start = typeof start !== 'undefined' ? start : new Date();
if (!(start instanceof Date))
throw "This is not a valid Date";
console.log('\nClient object:\n', JSON.stringify(client, null, 2));
TrackedTime.create({
start: start,
end: null,
title: title,
description: description
}).then(function (tt) {
console.log('\nTrackedTime object: \n' + JSON.stringify(tt, null, 2));
tt.setClient(client); // exception here!
});
}
我得到的例外是这一个:
Unhandled rejection TypeError: undefined is not a function
at startTimeTrack (C:\Users\admin\dev\tim\bin\cmd.js:255:5)
at C:\Users\admin\dev\tim\bin\cmd.js:221:6
at null.<anonymous> (C:\Users\admin\dev\tim\bin\cmd.js:285:4)
at tryCatcher (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\util.js:26:23)
at Promise._settlePromiseFromHandler (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\pr
omise.js:503:31)
at Promise._settlePromiseAt (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:
577:18)
at Promise._settlePromises (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:6
93:14)
at Async._drainQueue (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\async.js:123:16)
at Async._drainQueues (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebird\js\main\async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\admin\dev\tim\node_modules\sequelize\node_modules\bluebir
d\js\main\async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:367:17)
我真的不知道我在这里做错了什么。文档就像我做的那样here。我还尝试了该方法的多种变体(setClients
,addClient
)。
如何使用sequelize将相关对象正确添加到数据库?
编辑:
这是我从数据库接收的客户端对象:
Client object:
{
"id": 3,
"name": "Name Surname",
"street1": "Street 15",
"street2": "",
"zip": "12345",
"city": "City",
"short": "ms",
"createdAt": "2015-09-04T13:48:18.980Z",
"updatedAt": "2015-09-04T13:48:18.980Z"
}
注意:我继续使用这个小型(私有)项目,我只是使用node-sqlite3手动处理我的4个表。如果你来到这里,有同样的问题,其中一个答案对你有所帮助,请给我一个提示,我会接受它作为答案。
答案 0 :(得分:1)
FindAll将返回客户端数组,因此如果您只想返回一个客户端,则应使用findOne。
然后根据Sequelize Associations docs,您可以使用createAssociation,在您的情况下(未经测试):
function startTimeTrack(client, title, description, start) {
//....
//console.log(client.createTrackedTime) --> must return function
client.createTrackedTime({
start: start,
end: null,
title: title,
description: description
})
.then(function (tt) {
console.log(tt.get({plain:true}));
})
.catch(function (error){
console.log(error)
});
}