我正在为Node.js测试不同的ORM,并且遇到了这个错误:
Possibly unhandled TypeError: undefined is not a function
@ person.setUser(user);
尝试person.setUsers
,user.setPerson
和user.setPeople
。还试过console.log找不到运气的功能。
我做错了什么?
var config = require('./config.json');
var Sequelize = require('sequelize');
var sequelize = new Sequelize(config.connection, {
define: {
freezeTableName: true,
underscoredAll: true,
underscored: true
}
});
var Person = sequelize.define('person', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
var User = sequelize.define('user', {});
Person.hasOne(User);
sequelize.sync().then(run);
function run() {
var person = Person.create({ first_name: 'Markus', last_name: 'Hedlund' });
var user = User.create();
person.setUser(user);
}
答案 0 :(得分:4)
我认为你想要做的是
Person.create({ first_name: 'Markus', last_name: 'Hedlund' }).then((person) => {
User.create().then((user) => {
person.setUser(user);
});
});
答案 1 :(得分:4)
虽然dege的答案是正确的,但我会用一个很好的承诺链写出来:
Person.create({ first_name: 'Markus', last_name: 'Hedlund' })
.bind({})
.then(function(person){
this.person = person;
return User.create()
})
.then(function(user){
return this.person.setUser(user);
});