我试图并行创建2个用户并在做其他事情后等待创建。我试图使用:
Promise.all([User.create(usr1), User.create(usr2), User.create(usr3)]).then(function(){
console.log("\o/");
}
没有运气,因为User.create()
没有返回Promise。
我也尝试过:
User.create([usr1, usr2]).then(function(){
console.log('x')
})
我得到错误:
[Error (E_VALIDATION) 1 attribute is invalid] Invalid attributes sent to undefined:
• user
• A record with that user already exists (null).
正在运行的丑陋代码:
User.create([usr1]).then(function(){
User.create[usr2].then(function(){ console.log('x') });
});
也可以使用.exec()
User.create([usr1, usr2]).exec(function(){
console.log('x')
})
如何使用Promise或仅使用水线创建两个用户?
修改 1)在mocha测试中调用它
attributes: {
name: {
type: 'string',
required: true
},
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
},
//Association One-to-One, but using 'collection' to mantain sync updating
employer: {
collection: 'employer',
via: 'user'
},
employee: {
collection: 'employee',
via: 'user'
},
//Google Signin ID
googleId: 'string',
//Access token from the Google Authorization Server
googleAccessToken: 'string',
resetPasswordToken: String,
resetPasswordExpires: Date,
toJSON: function() {
var obj = this.toObject();
delete obj.password;
delete obj.resetPasswordToken;
delete obj.resetPasswordExpires;
return obj;
}
},
修改 2
只有在sails-mongo work中使用sails-disk时才会出现此错误