Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor if document exists return true/false

Tags:

meteor

How can I determine if a document exists within a collection in Meteor?

Edited: New code.

the mongodb has a document with the ProductName: Apples the inputproduct is "Apples"

var exists = Products.find({ProductName: inputproduct});
                    if(exists)
                    {
                        alert("it exists");
                    }else{
                        alert('doesnt exist');
                    }

all I get back is : "it exists" regardless of the value of inputproduct. I have output what inputproduct is and it comes back "Apples" no problem. Not sure what is going on here. Tried it several ways using find or findOne and nothing.

like image 274
Lusty Avatar asked Oct 19 '22 05:10

Lusty


1 Answers

You almost had it. However, meteor's collection.findOne will return the first object which matched, or undefined (which is falsy) if no match was found. Try this:

var exists = Products.findOne(selector, projection);
if(exists)
 {
    do something...
 }
like image 92
GPicazo Avatar answered Dec 01 '22 11:12

GPicazo