SyntaxError: Unexpected token o in JSON at position 1

I was working on Delete Many Documents with model.remove(). I got a different error something like this ;

SyntaxError: Unexpected token o in JSON at position 1.

I tried different things to solve but at the end i couldn’t find what’s wrong?

Your project link(s)

solution: https://replit.com/@halfgork/boilerplate-mongomongoose-3

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Delete Many Documents with model.remove()

Link to the challenge:

db.collection.remove(
  <query>,
  <justOne>
)

This is the signature of the .remove() method - it takes the query object, and a boolean that determines whether to remove the first matching record (if true) or all matching (if false, which is default).

You are calling .remove() like this:

Person.remove({name: nameToRemove}, (err, data) => {
  done(err, data);
  })

passing the query object, and a function. I suspect the unexpected token error is happening because that second parameter is throwing out an error code.

The Mongo docs are very useful here: docs.mongodb.com/manual/reference/method/db.collection.remove/ - and in particular, look at both the parameters the function takes, and what the function returns. That writeResult will tell you if you’ve had an error or not.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.