1

The problem is that when I start the server I get the error listed below. I'm pretty new to JavaScript, is this due to a difference in JavaScript or Nodejs versions? I also tried changing the var update to have [] instead of {}, but which gets the server to start, but then it won't update/delete data from our MongoDB. If it helps, the "Recipes" in Recipes.findOneAndUpdate is a Mongoose schema.

Here is the function from the server.js:

app.post("/updaterecipe", function(req, res) {
var id = req.body.recipeID;
console.log("Updating recipe " + id);
var recipeName = req.body.recipeName;
var categoryID = req.body.categoryID;
var recipeInstructions = req.body.recipeInstructions;
var ingredientIDs = req.body.ingredientIDs;
var options = {new: false};
var update = {recipeName, categoryID, recipeInstructions, ingredientIDs};
console.log(update);
Recipes.findOneAndUpdate({recipeID: id}, update, options, function(err) {
    if (err)
    {
        console.log("Unable to update");
        console.log(err);
    }
});
res.send(update);
});

And the error:

var update = {recipeName, categoryID, recipeInstructions, ingredientIDs};
                        ^

SyntaxError: Unexpected token , at exports.runInThisContext (vm.js:73:16) at Module._compile (module.js:443:25) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.j

5
  • what version of node? Commented May 21, 2016 at 16:06
  • Node version is v0.12.1 Commented May 21, 2016 at 16:19
  • 1
    update it, you cant use that syntax in the older node versions, go to 6+ Commented May 21, 2016 at 16:19
  • The update fixed it, thanks! My version was super old. Commented May 21, 2016 at 16:36
  • Awesome, I'll post it as the answer! Commented May 21, 2016 at 16:36

3 Answers 3

2

This is ES6 property shorthand:

var update = {recipeName, categoryID, recipeInstructions, ingredientIDs};

source: http://es6-features.org/#PropertyShorthand

Simply upgrade to the latest version of node and you should be good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will replace OP document in the collection.
0

I think there is a problem with your JSON. You are trying to create an invalide JSON object.

The following change to var update should do the trick.

var update = {
        "recipeName": recipeName,
        "categoryID": categoryID,
        "recipeInstructions": recipeInstructions,
        "ingredientIDs": ingredientIDs
     };

1 Comment

while this works, what he's doing is fine in the es6, but its not supported in his version of node
0

Your update document is not valid in ES5 because as already mentioned here, the Shorthand property names is new in (ES6), so you need to specify the "key"'s name or upgrade to a Nodejs version that use ES6 . That being said you also need to use the $set update operator because if you fail to use an $-modifier, your query will do a full-document replacement, replacing the matched document with the value of update.

var update = { 
    "$set": {
        "recipeName": recipeName,
        "categoryID": categoryID,
        "recipeInstructions": recipeInstructions,
        "ingredientIDs": ingredientIDs
    }
};

Then:

Recipes.findOneAndUpdate({ recipeID: id }, update, options, function(err) {
    if (err)
    {
        console.log("Unable to update");
        console.log(err);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.