Reputation: 932
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
Upvotes: 1
Views: 1906
Reputation: 61225
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);
}
});
Upvotes: 0
Reputation: 13888
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.
Upvotes: 2
Reputation: 112
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
};
Upvotes: 0