9

Good day for everyone. I have a strange error working with mongoose

module.js:437
  var compiledWrapper = runInThisContext(wrapper, filename, tru
                        ^
SyntaxError: Unexpected token .
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (E:\Dropbox\Dropbox\FCP\server.js
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

I gues it's goes from

dbQueries.remove({_id: {$in: {req.body.data}}, authorId: req.user._id}, function onRemoveSomething(err){
            if(err) {
                res.json({epicFail: 'ERR_RestrictedAccess'});
                return; 
            }
        });

So, I have no idea what is wrong.

Roman
  • 215
  • 1
  • 2
  • 10

3 Answers3

37

$in takes an array, not an invalidly formatted javascript object

{_id: {$in: [req.body.data]}

or if req.body.data is already an array, omit the wrapping [ ]

malix
  • 3,566
  • 1
  • 31
  • 41
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • Holly syncasis... Thanks a lot – Roman Aug 07 '12 at 08:00
  • 1
    After a few restarts of node it works fine with {_id: {$in: req.body.data} – Roman Aug 07 '12 at 18:40
  • @andy-ray How to do this when req.body.data has one element? Basically the contents of req.body.data can be a single element or an array is the requirement and I am using the in operator. How should I handle this situation? – godimedia Mar 23 '15 at 16:36
  • 1
    Why is this answer being upvoted so much? Is there some tutorial which has the broken syntax listed in the original question? – Andy Ray Jan 28 '16 at 23:54
  • stop upvoting this answer – Andy Ray Nov 26 '18 at 05:08
0

you have to check req.body.data is an array or not, see the code below {_id: {$in: _.isArray(req.body.data) ? req.body.data : [req.body.data] } //const _ = require('lodash');

0

Build your query based on the data

var match = {
    authorId: req.user._id
};

if(Array.isArray(data)) {
    match._id = {$in: data};
} else {
   match._id = data;
}

dbQueries.remove(match, function findMyDocs(err, foundDocs) {

});
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57