我想返回保存在用户集合下的完整数组[votes]。这是 JSON 结构
{
"_id" : "pziqjwGCd2QnNWJjX",
"createdAt" : ISODate("2017-12-21T22:06:41.930Z"),
"emails" : [
{
"address" : "test@test.com",
"verified" : false
}
]
"votes" : [
{
"ZyYZ4LDTaeWNMN9eE" : "yes"
},
{
"DSHhkdsjkdhsddsqd" : "no"
}
]
}
如何控制台.log该阵列?目标是在插入之前检查它是否存在。
如果您只想检查数组是否存在并具有元素:
const user = Meteor.users.findOne(userId);
const hasVotes = user && user.votes && typeof(user.votes) === 'object' && user.votes.length;
我假设您只从 MongoDB 中的集合中返回 1 个文档。
var user = Users.findOne({_id: your_given_id});
if(user && user.votes && user.votes.length){
console.log(user.votes);
return user.votes;
}
console.log('No Votes Found.');
return [];