尝试将电话号码与用户关联会导致节点告诉我用户对象没有方法 addPhonenumber。(也发生在 setPhonenumber 上)
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: 'xxx@gmail.com',
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: 'xxx@gmail.com',
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 }
我错过了什么?
谢谢!