访问用户Collection中的数组。[Meteor + React Native]

时间:2018-05-16 21:05:54

标签: javascript arrays meteor

我想返回保存在User Collection下的完整数组[votes]。 这是JSON结构

{
    "_id" : "pziqjwGCd2QnNWJjX",
    "createdAt" : ISODate("2017-12-21T22:06:41.930Z"),
    "emails" : [ 
        {
            "address" : "test@test.com",
            "verified" : false
        }
    ]
    "votes" : [ 
        {
            "ZyYZ4LDTaeWNMN9eE" : "yes"
        },
        {
            "DSHhkdsjkdhsddsqd" : "no"
        }
    ]
}

我怎么能在console.log那个数组?目标是在插入之前检查它是否存在。

2 个答案:

答案 0 :(得分:0)

我假设您只返回MongoDB中的一个文档。

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 [];

答案 1 :(得分:0)

如果您只想检查数组是否存在且是否包含元素:

const user = Meteor.users.findOne(userId);
const hasVotes = user && user.votes && typeof(user.votes) === 'object' && user.votes.length;