Reputation: 19540
I'm trying to add an object with a related object to the sequelize database but I can't figure out how to do it properly. I've read the documentation but I don't seem to have the methods on my objects they're using.
My model looks like this:
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();
First I search for the client using arguments read from commandline args (this works):
Clients.findAll({ where: { name: arg }}).then(function (clients) {
startTimeTrack(clients, argv.t, argv.d, start);
});
And finally, the method that does not work as I've expected when I read the docs is this one:
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!
});
}
The exception I get is this one:
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)
I really don't know what I'm doing wrong here. The documentation did it like I did it here. I also tried multiple variations of that set-method (setClients
, addClient
).
How do I properly add related objects to the database using sequelize?
Edit:
This is the client object I'm receiving from the database:
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"
}
Notice: I moved on with this small (and private) project and I'm just using node-sqlite3 to handle my 4 tables manually. If you, who landed here, have the same problem and one of the answers helped you, give me a hint and I'll accept it as answer.
Upvotes: 0
Views: 624
Reputation: 1213
FindAll will return array of clients, so if you want to return just one client you should use findOne.
Then according with Sequelize Associations docs , you could use createAssociation , in your case (not tested) :
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)
});
}
Upvotes: 1