我在 node.js 和 socket.io 工作,基本上我监听一个套接字 disconnect
事件并调用一个方法。 app.update()
socket.on('disconnect', function (data) {
app.update();
});
// This is in another file, and the id is static, but I am more concered with the
// messages object that is passed through.
update: function() {
models.Message.find({_id: "532c8d9ce889ed4c21538630"}, function(err, messages) {
console.log(messages)
});
}
这就是messages
对象的样子
[ { username: 'Marcus',
connected: true,
_id: 532c8d9ce889ed4c21538630,
__v: 0 } ]
我不确定如何访问带有方括号的对象,但基本上我想更新该对象,以便我可以将 connected
设置为 false。
我尝试了 messages['connected']
。
最佳答案
您有一个对象数组。因此,您需要访问数组的第一个元素,然后是“已连接”属性。这样做:
messages[0].connected = false;
关于javascript - 如何访问此对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567801/