Delete Many Documents error

Tell us what’s happening:
I’m getting this error message when I submit my code for testing:

SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at /home/runner/boilerplate-mongomongoose/server.js:346:29
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/model.js:4919:18
    at processTicksAndRejections (internal/process/task_queues.js:79:11)

Can someone explain how I fix it? Thanks.

Your project link(s)

solution: https://replit.com/@mattnk/boilerplate-mongomongoose

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Delete Many Documents with model.remove()

Link to the challenge:

You’re trying to parse something that’s already a JavaScript object.

The reason it says what it says in the error message is that “o” is for “object”. When parse runs it’s converting the object to a string, which produces [object Object]. Arrays are valid as JSON, but that isn’t an array, it’s the identifier you get back when a JS object is converted to a string using toString. So

function parsingObjExample () {
  const obj = { name: "Ex Ample" };
  
  try {
    const thisWillFail = JSON.parse(obj);
  } catch (err) {
    console.error(err);
  }
}

parsingObjExample()

This function will log an error identical to yours (I’m running the function on Node on a phone, so the stack trace is different, but error is the same)

SyntaxError: Unexpected token o in JSON at position 1
    at Object.parse (native)
    at parsingObjExample (/data/user/0/io.tempage.dorynode/cache/dory-8848310032880871981.js:5:31)
    at Object.<anonymous> (/data/user/0/io.tempage.dorynode/cache/dory-8848310032880871981.js:11:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:383:7)
{ name: "Ex Ample" }
// Then:
JSON.parse({ name: "Ex Ample" })
// Then JSON.parse expects a string,
// so it does this to make sure:
JSON.parse({ name: "Ex Ample" }.toString())
// This produces:
JSON.parse("[object Object]")
// The character at position 1 in the
// string is "o", invalid JSON, error

Look at the line number in the server file, and try again after replacing the closest JSON.parse call with just the thing that you’re parsing (I can’t check exactly which call is doing this, as I say, on phone so a bit difficult)

I changed the code in server.js from data = JSON.parse(data); to data.

That got rid of the syntax error but the solution still not successful. I tried using the following solution suggestion in myApp.js:

const removeManyPeople = (done) => {
  const nameToRemove = "Mary";
  Person.remove({name: nameToRemove}, (err, response) => {
    if(err) return console.log(err);
    done(null, response);
  })
};

But I still get a console message saying that remove is depreciated and a message in the tutorial saying the the solution is failing to delete many items. I tried changing remove to deleteMany and get the same response.

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