writeConcern requires callback [newbie]

329 views
Skip to first unread message

Gregory Edigarov

unread,
Dec 19, 2014, 11:30:53 AM12/19/14
to nod...@googlegroups.com
Hello,

Error: writeConcern requires callback
    at updateWithWriteCommands (/home/greg/nodejs-module/app/server/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:530:11)
    at Collection.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:646:12)
    at NativeCollection.(anonymous function) [as update] (/home/greg/nodejs-module/app/server/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:135:28)
    at NodeCollection.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/node_modules/mquery/lib/collection/node.js:61:19)
    at Query.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/node_modules/mquery/lib/mquery.js:1939:20)
    at Query.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/lib/query.js:1760:28)
    at Function.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/lib/model.js:1638:13)
    at model.update (/home/greg/nodejs-module/app/server/node_modules/mongoose/lib/document.js:367:34)
    at onPostMenuUpdate (/home/greg/nodejs-module/app/server/requests/postRequests.js:74:10)
    at Layer.handle [as handle_request] (/home/greg/nodejs-module/app/server/node_modules/express/lib/router/layer.js:82:5)

the related function is:

var onUpdate = function (err, num, raw) {
    if (!err) {
    logger.info('information stored');
    return myResponse.send ({status: 'ok' });
    } else {
    logger.info (err);
    if(err.name == 'ValidationError') {
            myResponse.statusCode = 400;
            myResponse.send({ error: 'Validation error'});
            logger.info(req.body);
        } else {
            myResponse.statusCode = 500;
            myResponse.send({ error: 'Server error' });
        }
    logger.error('internal error(%d):%s');
    }

 

function onPostMenuUpdate (request,response) {
   logger.info("In onPostMenuUpdate");
    myResponse=response;
    var menu = new model.MainMenuModel ({
    'name': request.body.name,
    'link': request.body.link,
    'drop': request.body.drop
    });
    var menuObject=menu.toObject();
   
    menu.update({name:request.body.name}, menuObject, {upsert: true}, onUpdate);
}

How can i avoid this error.

Zach Rollyson

unread,
Dec 19, 2014, 1:46:16 PM12/19/14
to nod...@googlegroups.com
Comes from the line "menu.update({name:request.body.name}, menuObject, {upsert: true}, onUpdate)"

It's because mongoose update takes three arguments: document, options, callback.  You have object, object, options, callback; thus mongoose thinks your options object is the callback.

rewrite as:


var onUpdate = function (err, num, raw) {
    if (!err) {
    logger.info('information stored');
    return myResponse.send ({status: 'ok' });
    } else {
    logger.info (err);
    if(err.name == 'ValidationError') {
            myResponse.statusCode = 400;
            myResponse.send({ error: 'Validation error'});
            logger.info(req.body);
        } else {
            myResponse.statusCode = 500;
            myResponse.send({ error: 'Server error' });
        }
    logger.error('internal error(%d):%s');
    }
}  
 

function onPostMenuUpdate (request,response) {
   logger.info("In onPostMenuUpdate");
    myResponse=response;
    var menu = new model.MainMenuModel ({
    'name': request.body.name,
    'link': request.body.link,
    'drop': request.body.drop
    }); 
    var menuObject=menu.toObject();

    menuObject.name = request.body.name
    
    menu.update(menuObject, {upsert: true}, onUpdate);
}

& that should work

Zach Rollyson

unread,
Dec 19, 2014, 1:48:47 PM12/19/14
to nod...@googlegroups.com
P.S., to find these things yourself, think about what "Error: writeConcern requires callback" means (probably that you're missing a callback for something that writes [only thing that writes here is menu.update]), and look at the documentation for mongoose http://mongoosejs.com/docs/api.html#document_Document-update.


On Friday, December 19, 2014 11:30:53 AM UTC-5, Gregory Edigarov wrote:

Ryan Graham

unread,
Dec 20, 2014, 9:40:38 PM12/20/14
to nodejs
On Fri, Dec 19, 2014 at 10:46 AM, Zach Rollyson <za...@izimobile.com> wrote:
Comes from the line "menu.update({name:request.body.name}, menuObject, {upsert: true}, onUpdate)"

It's because mongoose update takes three arguments: document, options, callback.  You have object, object, options, callback; thus mongoose thinks your options object is the callback.


I nearly replied with a similar response, but then I realized the code is actually calling Model#update, not Document#update. The former actually does take 4 arguments and from a cursory look it seems the calling code is using it more or less correctly.

That said, I agree that changing this to Document#update is probably still the correct solution.

~Ryan
-- 

Zach Rollyson

unread,
Dec 22, 2014, 10:09:48 AM12/22/14
to nod...@googlegroups.com
How exactly do you figure it's calling Model.update?  Because of the stack trace?  I think mongoose calls model.update internally, perhaps?  I just tested, when updating a document without a callback (i.e. I replicated his call) the stack includes a call to model.update.  I'm pretty sure he is calling document.update, and the error is because he has too many arguments.  

Ryan Graham

unread,
Dec 22, 2014, 7:47:27 PM12/22/14
to nodejs
You are correct. I think I distracted myself with the links I provided and forgot why I pasted them, which was to show the OP where their confusion likely came from.

Thanks for calling me on it so the misleading information wasn't left as the final word.

~Ryan

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/b66f71da-f526-4b24-a017-14a216b5e6ec%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Zach Rollyson

unread,
Dec 23, 2014, 10:59:33 AM12/23/14
to nod...@googlegroups.com
Ahhh yes that does make a lot of sense, could definitely see OP (or anyone) getting confused if they came across the model.update docs before the document.update ones.  Thank *you* for being so exceedingly polite & respectful, you're awesome. :)
Reply all
Reply to author
Forward
0 new messages