I'm having trouble receiving a list of items that match with my array of ids.
Here's a snippet from the Angular component code:
this.orderService.getSpecyficOrders(ids)
.subscribe(orders => { ...
Where ids is an array of
[{_id : ID },{_id : ID },{_id : ID },]
The ID is a string in the format "5235sd23424asd234223sf44"
taken from MongoDB documents.
In the Angular service file, I have imported: Http, Headers, and import 'rxjs/add/operator/map';
Below is the code in the Angular service:
getSpecyficOrders(ids){
return this.http.get('/api/ordersspecyfic', ids)
.map(res => res.json());
}
In the Express file, I have required: multer, express, router, mongojs, db
And here is part of the code in Express for calling to MongoDB:
router.get('/ordersspecyfic', function(req, res, next){
var ids = req.body;
ids = ids.map(function (obj){ return mongojs.ObjectId(obj._id)});
db.orders.find({_id: {$in: ids}}, function(err, orders){
if(err){
res.send(err);
}
res.json(orders);
});
});
However, I'm encountering an error:
Uncaught Response {_body: "TypeError: ids.map is not a function
&n…/node_modules/express/lib/router/index.js:46:12)↵", status: 500, ok: false, statusText: "Internal Server Error", headers: Headers…}
When logging req.body
in the Express file,
it shows me that it is an empty object {}
I understand that req.body
is not an array by default, but I'm unsure if that's the only issue with the code.
All other requests like getting a single element or all items are working fine. I just can't seem to get this one to work properly.