I'm currently creating a MEAN application to allow a user to create a football team from a list of ownedPlayers. Currently however when I attempt to make a http.get call to the ownedPlayers collection in MongoDB it always returns an empty array.
My mongoose model for OwnedPlayers is as follows.
var OwnedPlayers = mongoose.model('OwnedPlayers', {
userid: String,
playerid: String,
position: String
});
The endpoint and query is as follows.
app.get('/api/ownedPlayers', function (req, res) {
console.log('userid used in request', req.query.userid)
console.log('position used in request', req.query.position)
OwnedPlayers.find({
userid: req.query.userid, position: req.query.position
}, function (err, owned)
{
if (err)
{
res.send(err);
}
console.log('owned',owned);
res.json(owned);
});
});
I can confirm that the values in req.query.userid and req.query.position do match a single record in my ownedPlayers collection. This can be seen by my nodejs output below.
App listening on port 8080
GET /api/teams/57f50267418cb72fd020196a 304 31.936 ms - -
userid used in request 57f50267418cb72fd020196a
position used in request goal
owned []
GET /api/ownedPlayers?userid=57f50267418cb72fd020196a&position=goal 304 32.903 ms - -
I can also confirm that the key names are correct as userid and position.
The document I would expect to be returned when userid = 57f50267418cb72fd020196a and position = position= goal is as follows.
_id:57f7717ab1f0f22f3c3950b9
userid:57f50267418cb72fd020196a
playerid:57f7797e00ea2a0dc8609701
position:goal
Any help would be much appreciated as I'm very new to mongodb and mean applications.
Thanks in advance.
The problem was that since I was using an collection with documents which weren't created via mongoose it wasn't finding the collection correctly.
By passing in the collection name as a third parameter when creating my models this explicitly sets the collection name and points mongoose in the right direction.
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.