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.
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With