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)
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
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)
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.