Javascript 数组类型为won'的mongodb字段;t使用.push()时更新长度
以下是尝试将用户添加到架构的代码:Javascript 数组类型为won'的mongodb字段;t使用.push()时更新长度,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,以下是尝试将用户添加到架构的代码: var roomSchema = new Schema({ name: { type: String, required: true}, connections: { type: [ { userId: String } ] }, content: { type: String, default: ""}, isFull: { type: Boolean, default: false} }); roomSchema.meth
var roomSchema = new Schema({
name: { type: String, required: true},
connections: { type: [ { userId: String } ] },
content: { type: String, default: ""},
isFull: { type: Boolean, default: false}
});
roomSchema.methods.addUser = function(username, callback) {
this.updateAsync( { $push: { connections: { userId: username } } } )
.then(function(status) {
return this.saveAsync();
})
.catch(function(afterPushErr) {
console.log('After push error');
throw afterPushErr;
})
.catch(function(saveErr) {
console.log('save Error');
throw saveErr;
});
Promise.all(this.connections)
.then(function() {
if(this.connections.length >= 5) {
this.updateAsync( { isFull: true })
.then(function(status) {
return;
})
.catch(function(updateErr) {
console.log('update Error!');
throw updateErr;
});
}
});
}
然后是调用它的代码(正确导入上述函数):
(注意:这只是一个快速测试功能,以确保每个房间最多只有5个用户)
编辑
这是addUser的承诺代码上的新错误
(节点:4648)未处理的PromisejectionWarning:未处理的承诺
拒绝(拒绝id:1):类型错误:无法读取属性
未定义的“连接”(节点:4648)
未处理的PromisejectionWarning:未处理的承诺拒绝
(拒绝id:2):TypeError:无法读取的属性“connections”
推送错误后未定义推送错误后保存错误保存错误
未处理的拒绝类型错误:无法读取的属性“saveAsync”
未定义
在C:\someApp\app\models\room model.js:19:14
在tryCatcher(C:\someApp\node\u modules\bluebird\js\release\util.js:16:23)
在测试函数中,将异步调用(
Room.findOne
)放入for循环中。因此,每个循环都有相同的freeZoom
。(这不是您想要的)
检查此问题:
另一个建议是,
addUser
函数中的this.update
也是异步的,在某些情况下可能与您想要的不一样。因此,问题是前5个查询将返回相同的查询;在他们找到一个房间后,会添加一个用户,并保存房间和用户。最后的第一个房间有6个用户,这意味着唯一缺少的是房间满时推送的停止点(里面有5个人)。也许您可以使用mongoose查询作为,因此,我能够获得脚本以异步方式添加用户(大概是这样,我将编辑上面的代码以反映这一点。但是,看起来错误的核心是addUser函数,并确保一切按顺序进行。我还将显示新代码以反映这些更改,我得到一个新错误:无法保存未定义的属性。
var populateRooms = function() {
var names = [
'asas',
'asas2',
'asas3',
'asas4',
'asas5',
'asas6'];
var emails = [
'asas@as.ca',
'asas2@as.ca',
'asas3@as.ca',
'asas4@as.ca',
'asas5@as.ca',
'asas6@as.ca'];
for(var i=0; i<6; ++i) {
Room.findOneAsync( { isFull: false })
.then(function(freeRoom) {
var newUser = new User({
username : names[i],
email : emails[i],
password : 'Asasas1',
isPlaced: true,
roomName: freeRoom.name
});
freeRoom.addUser(newUser.username);
return newUser;
})
.then(function(newUser) {
newUser.saveAsync();
})
.catch(function(err) {
throw err;
});
}
return true;
}
{ "_id" : ObjectId("5882c3eefab3081700444972"), "name" : "1484964846968_0", "isFull" : false, "content" : "", "connections" : [ { "userId" : "asas5", "_id" : ObjectId("5882c3effab308170044497f") }, { "userId" : "asas6", "_id" : ObjectId("5882c3effab308170044497d") }, { "userId" : "asas4", "_id" : ObjectId("5882c3effab3081700444981") }, { "userId" : "asas6", "_id" : ObjectId("5882c3effab308170044497d") }, { "userId" : "asas5", "_id" : ObjectId("5882c3effab308170044497f") }, { "userId" : "asas4", "_id" : ObjectId("5882c3effab3081700444981") }, { "userId" : "asas3", "_id" : ObjectId("5882c3effab3081700444983") }, { "userId" : "asas", "_id" : ObjectId("5882c3effab3081700444987") }, { "userId" : "asas2", "_id" : ObjectId("5882c3effab3081700444985") }, { "userId" : "asas3", "_id" : ObjectId("5882c3effab3081700444983") }, { "userId" : "asas2", "_id" : ObjectId("5882c3effab3081700444985") }, { "userId" : "asas", "_id" : ObjectId("5882c3effab3081700444987") } ], "__v" : 12 } { "_id" : ObjectId("5882c3eefab3081700444973"), "name" : "1484964846978_1", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444974"), "name" : "1484964846980_2", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444975"), "name" : "1484964846980_3", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444976"), "name" : "1484964846981_4", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444977"), "name" : "1484964846981_5", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444978"), "name" : "1484964846982_6", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab3081700444979"), "name" : "1484964846984_7", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab308170044497a"), "name" : "1484964846984_8", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 } { "_id" : ObjectId("5882c3eefab308170044497b"), "name" : "1484964846984_9", "isFull" : false, "content" : "", "connections" : [ ], "__v" : 0 }