简体   繁体   中英

meteor collections field search

I grab the currently selected mongo document using 'this' inside of a Template.alerts.events function. I then want to check to see if the current document (this) contains a specific field. To be precise, I want to see if it contains the userId which I store there myself previously.

doc {
    field1: 'a'

    myfield: {
        userId1: timestamp
        userId2: timestamp
        ...
    }

}

I basically want to see if the current user's userId is in the userId fields of myfield.

I have tried:

var x = this.userId
if(this.myfield.x) {
    //do something
}

and I'm wondering whether it is because I need to wrap userId in a string or something, but I can't get the condition to evaluate. I have also tried using object.hasOwnProperty which also didn't work. How can I do this in a non query way?

I'm assuming that you're trying to see if 'x' is a field in the object this.myField . If so, you have to do something like this: if (this.myfield[this.userId]) . If you're trying to compare x and this.myfield , then you need to do if (this.userid == this.myfield)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM