2015-09-04 52 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