无法在键为数字的 MongoDB 文档中引用 key/value 对
Can't reference key/value pair in MongoDB document where the key is a number
我将数据库中文档的键设置为数字。我想引用密钥来获取值,但是,无论我尝试什么,它 returns 都未定义。
for(let x = 0; x < channelList.length; x++) {
var chanQuery = channelModel.findOne({channelID: channelList[x]}, "channelName", function(err, result) {
if(err) {console.log(err)};
return result;
});
var datQuery = userModel.findOne({userID: userList[i]}, function(err, result) {
if(err) {console.log(err)};
return result;
});
chanQuery.then(function(doc) {
datQuery.then(function(doc2) {
msgChannel.send(doc.channelName + ": " + doc2[channelList[x]];
});
});
Here 是我尝试访问的文档的屏幕截图。
已解决!我需要使用 .toJSON() 将文档转换为 json,以便将密钥引用为数字。
我将数据库中文档的键设置为数字。我想引用密钥来获取值,但是,无论我尝试什么,它 returns 都未定义。
for(let x = 0; x < channelList.length; x++) {
var chanQuery = channelModel.findOne({channelID: channelList[x]}, "channelName", function(err, result) {
if(err) {console.log(err)};
return result;
});
var datQuery = userModel.findOne({userID: userList[i]}, function(err, result) {
if(err) {console.log(err)};
return result;
});
chanQuery.then(function(doc) {
datQuery.then(function(doc2) {
msgChannel.send(doc.channelName + ": " + doc2[channelList[x]];
});
});
Here 是我尝试访问的文档的屏幕截图。
已解决!我需要使用 .toJSON() 将文档转换为 json,以便将密钥引用为数字。