mongo / node TypeError:callback不是查询的函数 [英] mongo/node TypeError: callback is not a function on query

查看:227
本文介绍了mongo / node TypeError:callback不是查询的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定文档是否存在于集合中。如果文档存在,我希望向对象添加属性unread = false。如果它不存在,我希望插入文档并将unread = true添加到对象。

I am trying to determine if a document exists in a collection. If the document exists, I wish to add a property "unread = false" to an object. If it does not exist, I wish to insert the document and add "unread = true" to the object.

以上的咖啡脚本代码如下:

Code in coffee script for the above is as follows:

functionxyz = (db, uid, events, done) ->
async.each events, (eventobj) ->
    if db.Event.find(eventobj).count() > 0
        eventobj.unread = false
    else
        db.Event.insert eventobj
        eventobj.unread = true
done null, events

我收到的错误是

/Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/connection/base.js:246
        throw message;      
        ^

TypeError: callback is not a function
  at /Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/collection/commands.js:55:5
  at /Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/db.js:1197:7
  at /Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/db.js:1905:9
  at Server.Base._callHandler (/Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
  at /Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
  at [object Object].MongoReply.parseBody (/Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
  at [object Object].<anonymous> (/Users/owner/Desktop/coding challenge/repo/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
  at emitOne (events.js:77:13)
  at [object Object].emit (events.js:169:7)
  at [object Object].<anonymous> (/Users/owner/Deskto

有人可以向我解释此错误发生的原因,潜在的解决方案可能是?

Can someone explain to me the reason this error is occurring and what a potential solution might be?

推荐答案

Node的MongoDB本地驱动程序遵循Node.js约定的异步函数,接收一个回调函数作为最后一个参数,所以代替 db.collection.find(query).count(),你的函数应该改写为:

The MongoDB Native Driver for Node follows the Node.js convention for async functions, namely that each method receives a callback function as the last parameter. So instead of db.collection.find(query).count(), your function should be rewritten as:

db.collection.find(query).count( function(err, count){ // do stuff here } 

参数 count 捕获您的查询结果。

The parameter count captures your query's result.

您还可以将函数简化为 db.collection.count(query,function(err,count){}

You could also simplify the function to db.collection.count(query, function(err, count){}.

您的插入函数也应遵循相同的约定,使用 function(err,res){} 形式的回调函数作为最后一个参数。

Your insert function should also follow the same convention, using a callback function with form of function(err, res){} as the last parameter.

我建议您查看

I'd recommend looking at the MongoDB Native Driver Docs for more information.

编辑以在CoffeeScript中提供示例:

Edited to give example in CoffeeScript: Here's the function rewritten with CoffeeScript syntax.

db.Event.count(eventobj, (err, count) ->
    // do stuff

这篇关于mongo / node TypeError:callback不是查询的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆