I am attempting to locate the object containing a specific element within an array:
Here is my query:
router.get('/', function(req, res) {
var userSubscribed = req.user.email;
db.posts.find({ "SubscriberList": { $elemMatch: userSubscribed}}, function(err, object){
if (err) console.log(err);
else console.log(object);
res.render('index', { title: 'Home', user: req.user});
})
});
The error message displayed is as follows:
{ [MongoError: Can't canonicalize query: BadValue $elemMatch needs an Object] name: 'MongoError'}
An example of an object in the database:
{
"_id" : ObjectId("5490afa8416841105626cc41"),
"post" : {
"title" : "Ralph",
"category" : "Node",
"date" : "2014-12-11",
"description" : "Node coming soon!"
},
"owner" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1838e9492808c9380cf93808d9189a1868c80888dcf828e8c">[email protected]</a>",
"status" : "active",
"category" : "Node",
"SubscriberList" : ["<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0919c91829993b0989f849d91999cde939f9d">[email protected]</a>" ]
}