2015-09-04 41 views
0

我想添加一个对象与相关对象到sequelize数据库,但我无法弄清楚如何正确地做到这一点。我已经阅读过文档,但我似乎没有使用我的对象的方法。使用sequelize添加对象与数据库的关系

我的模型看起来是这样的:

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个表。如果你在这里登陆,遇到同样的问题,并且其中一个答案对你有帮助,给我一个提示,我会接受它作为答案。

回答

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) 
    }); 
} 
+0

对不起,我没有早些回来。我仍然得到一个异常'未处理的拒绝类型错误:undefined不是''createTrackedTime'调用发生的函数。 – wullxz

+0

你改变findAll for findOne了吗? – cshion

+0

是的,我做过。但它并没有改变。由此产生的客户端对象仍然没有作为成员的功能。我在我的问题中添加了'JSON.stringify(client)'的输出。 – wullxz