嘗試將電話號碼與用戶關聯會導致節點告訴我User對象沒有方法addPhonenumber。 (也與setPhonenumber一起發生)關聯對象關聯對象失敗並且沒有方法addX
req.user.setPhonenumbers(phonenumber).success(function(user){
^
TypeError: Object [object Object] has no method 'setPhonenumbers'
如何正確關聯對象?下面的代碼。在models.js
對象定義:
exports.User = require("./user")(sequelize, Sequelize);
exports.Phonenumber = require("./phonenumber")(sequelize, Sequelize);
exports.Recording = require("./recording")(sequelize, Sequelize);
exports.Call = require("./call")(sequelize, Sequelize);
User
.hasMany(Phonenumber)
.hasMany(Recording);
Phonenumber
.belongsTo(User)
.hasOne(Recording)
.hasMany(Call);
Recording.belongsTo(User);
Call.belongsTo(Phonenumber);
試圖設置關聯:
var phonenumber = yp.db.Phonenumber.create({
number: number.phone_number
}).success(function(number){
console.log(number);
console.log(req.user);
req.user.setPhonenumbers(phonenumber).success(function(user){
console.log(user);
});
});
和上述參考req.user的結果的console.log:
{ __options:
{ timestamps: true,
instanceMethods: { fullname: [Function] },
classMethods: {},
validate: {},
freezeTableName: false,
underscored: false,
syncOnAssociation: true,
paranoid: true,
omitNull: false,
hasPrimaryKeys: false },
hasPrimaryKeys: false,
selectedValues:
{ firstname: 'Nathan',
lastname: 'Loyer',
email: '[email protected]',
password: '****',
facebookid: '****',
id: 2,
createdAt: Thu Feb 28 2013 20:40:29 GMT-0600 (CST),
updatedAt: Wed Mar 06 2013 20:22:05 GMT-0600 (CST),
deletedAt: null },
firstname: 'Nathan',
lastname: 'Loyer',
email: '[email protected]',
password: '****',
facebookid: '****',
id: 2,
createdAt: Thu Feb 28 2013 20:40:29 GMT-0600 (CST),
updatedAt: Wed Mar 06 2013 20:22:05 GMT-0600 (CST),
deletedAt: null,
isNewRecord: false }
我錯過了什麼?
謝謝!
我已經更新了上面的關聯,並且使用了setPhonenumbers,儘管文檔在您指出的部分下面列出了addTask函數。但結果仍然相同。 – 2013-04-08 17:35:43
你能否用實際運行的文件替換你的例子。如果答案不明顯,我們需要真正能夠運行代碼來進行調試。 – dankohn 2013-04-08 20:13:59
既然你不能在沒有整個項目的情況下運行這個代碼,因爲這是一個客戶的財產,我無法分享完整的項目,你是否建議我只寫一個快速的頁面與這些關聯,並嘗試將其設置爲靜態值?如果這有效,我們將如何接近實際的解決方案? – 2013-04-10 17:13:04