4

I am having trouble returning getting the Objects in a collection based on an array of ObjectIds.

I have a collection of 10 items. In an iOS app, I am passing up an array of ObjectIds that user has saved as favorites to return just those two in a favorites tab, for example -

["59f85eae4545XXX9b94d53d3", "59f85eae45454XXXb94d76a1"]

For now, I have just hardcoded the ObjectIds at the top of the request using NodeJS just until I get the request working, below is the request -

exports.list_favs = function(req, res) {
  Articles.find({
    _id: { $in: [
     mongoose.Types.ObjectId('59f85aXXXXf1769b94d53ce'),
     mongoose.Types.ObjectId('59f85eaeXXXX1769b94d53d3')
 ]}
}, function(err, articles) {
if (err)
  res.send(err);
  res.json({
    data: {
      articles
    }
   });
 });
};

And I have even tried this -

exports.list_favs = function(req, res) {

var ids = new Array( new ObjectId("59f85eaeXXXX1769b94d53d3"), new 
ObjectId("59f85aXXXXf1769b94d53ce") );

Articles.find({
    _id: { $in: ids}
}, function(err, articles) {
if (err)
  res.send(err);
  res.json({
    data: {
      articles
    }
   });
 });
};

Both these give me an error "CastError"

"Cast to ObjectId failed for value \"favourites\" at path \"_id\" for model \"Articles\""

This is how it looks in the database so I am completely baffled as to what I am doing wrong. Image of ObjectId in database

enter image description here

I've followed multiple other answers on StackOverflow with no luck.

Gist to full project code

Marc Blazyk
  • 87
  • 1
  • 9
  • 2
    From your error message *failed for value \"favourites\"* which actually tells us this is being produced by passing in something different to what you say you are passing in. The error says you are passing in the "string" `"favourites"`. I'm guessing this is acutally JSON string representing a list, and you have that because of something you are doing wrong elsewhere in code not included in the question. – Neil Lunn Nov 01 '17 at 10:35
  • I see what you mean but I am not sure where it could be being passed in. I am simply calling the following in postman. app.route('/articles/favourites').get(articleList. list_favs) – Marc Blazyk Nov 01 '17 at 10:42
  • Thats all the code that is being used for that call other than the Schema class for Articles. – Marc Blazyk Nov 01 '17 at 10:51
  • What you are being told is your presented question shows "hardcoded" values. But the error message says otherwise. [This is not the code where the error is coming from](http://www.google.com.au/search?q=these+aren%27t+the+droids+you%27re+looking+for) and the error tells us so. By all means show the schema. But also check your code that you have not actually defined the same route "twice". The error message is very specific and clear. – Neil Lunn Nov 01 '17 at 10:52
  • I am not sure how that can be because I am currently running that second block of code with the correct ObjectIds and that is the exact result I am getting when I call it in Postman using http:// – Marc Blazyk Nov 01 '17 at 11:01
  • Tell you what. Transfer your code into a a single listing with just the schema, and necessary code to define a single express route in a way that reproduces the problem. If you can post that code here so we can all see the same problem, then that's something we can work with. But right now I'm telling you for a fact that the code you are showing us **cannot** produce the error message you are showing. – Neil Lunn Nov 01 '17 at 11:09
  • 1
    I see my error and still some confusion, I previously had a request which accepted a single id parameter which was like app.route('/articles/:id'). I have update my server and restarted and now it seems to work. Apologies for my confusion and lack of knowledge but thank you for your help Neil. – Marc Blazyk Nov 01 '17 at 12:51
  • Figured as much. You can search for similar things to that and note that the order you define the routes in is important. – Neil Lunn Nov 01 '17 at 12:55

1 Answers1

0

Do this way :

exports.list_favs = function(req, res) {
  Articles.find({
        _id: {
          $in: [mongoose.Schema.ObjectId('59f85aXXXXf1769b94d53ce'),
          mongoose.Schema.ObjectId('59f85eaeXXXX1769b94d53d3')]
      }
    },
    function(err, articles) {
      if (err)
        res.send(err);
      else
        res.json({
          data: {
            articles
          }
        });
    });
};
Vikash_Singh
  • 1,856
  • 2
  • 14
  • 27