在 Sequelize >=1.7中,我们可以使用Promise
你能为我解释一下如何在这段代码中从每个用户那里获取值:
var User = sequelize.define("user", {
username: Sequelize.STRING
})
User
.sync({ force: true })
.then(function() { return User.create({ username: 'John' }) })
.then(function(john) { return User.create({ username: 'Jane' }) })
.then(function(jane) { return User.create({ username: 'Pete' }) })
.then(function(pete) {
console.log("we just created 3 users :)")
console.log("this is pete:")
console.log(pete.values)
// what i want:
console.log("this is jane:")
console.log(jane.values)
console.log("this is john:")
console.log(john.values)
})
UPD
所有值都需要设置与其他模型的关联。其实我需要一些像这样的代码:
User.hasMany(Group)
Group.hasMany(User)
User
.sync({ force: true })
.then(function() { return User.create({ username: 'John' }) })
.then(function(john) { return User.create({ username: 'Jane' }) })
.then(function(jane) { return User.create({ username: 'Pete' }) })
.then(function(pete) { return Group.findOrCreate({id: 1}) })
.then(function(group) {return group.setUsers([john, jane, pete])})
.then(function(result) { console.log(result)})
})