mongoose - MongoDb Duplicate Entry Error -
hi why i'm getting duplicate entry errors. check in advance if document in database. model requires uniqueness idname
. load data json array, prooven false, there no entries true.
async.map recipe.zutaten, (ingredient, cb) -> #save ingredients ingredient.idname = ingredient.name.replace(/[^a-za-z0-9]+/gi, "").tolowercase() ingredientmodel.find({ idname: ingredient.idname }, (err, ingredientfound) -> return next err if err ingredientsjson = {"idname":ingredient.idname, "name":ingredient.name, "amount":ingredient.amount} #if found pass recipes if ingredientfound? && ingredientfound.length > 0 ingredientsjson.prooven = true return cb null, ingredientsjson #if not found evaluate if save else #if not prooven add json recipes if(ingredient.prooven? && ingredient.prooven == false) ingredientsjson.prooven = false return cb null, ingredientsjson #if prooven save database else ingredientdbobject = new ingredientmodel() ingredientdbobject.name = ingredient.name ingredientdbobject.idname = ingredient.idname ingredientdbobject.save((err) -> return cb err if err ingredientsjson.prooven = true return cb null, ingredientsjson ) ) ...
error
{ [mongoerror: insertdocument :: caused :: 11000 e11000 duplicate key error index: database.ingredients.$idname_1 dup key: { : "zitronensaft" }] name: 'mongoerror', message: 'insertdocument :: caused :: 11000 e11000 duplicate key error index: database.ingredients.$idname_1 dup key: { : "zitronensaft" }', index: 0, code: 11000, errmsg: 'insertdocument :: caused :: 11000 e11000 duplicate key error index: database.ingredients.$idname_1 dup key: { : "zitronensaft" }' }
the problem related async.map
not wait callback. use of async.mapseries fixed problem. map series waits cb resolved: https://github.com/caolan/async#mapseries
... async.mapseries recipes, (recipe, next) -> ... async.mapseries recipe.zutaten, (ingredient, cb) -> ...
Comments
Post a Comment