Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error: callback is not a function!" when trying to call findOne(...) #52

Closed
jamalex opened this issue Sep 1, 2011 · 5 comments
Closed

Comments

@jamalex
Copy link

jamalex commented Sep 1, 2011

When I try to do a simple query on a collection, such as:

var Mongolian = require("mongolian")
var server = new Mongolian
var db = server.db("test")
var docs = db.collection("docs")
docs.findOne({}, function(err, doc) { console.log(doc) })

I get the error:

Error: callback is not a function!
    at [object Object].next (/home/jamalex/node/node_modules/mongolian/lib/cursor.js:153:48)
    at [object Object].findOne (/home/jamalex/node/node_modules/mongolian/lib/collection.js:53:42)
    at [object Context]:1:6
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)

It does the same thing no matter where I put the callback function (1st, 2nd, or 3rd argument). I also get the same error when I try to run the findOne code from the main example. The find() code seems to work, though I too get a stack overflow (as others reported) when doing a forEach on a large cursor.

I'm using node v0.4.11, and mongolian 0.1.10 (the one npm just installed), on 64bit Ubuntu 11.04. Thanks!

@marcello3d
Copy link
Owner

Curious... I wonder if somehow Function in mongolian is a different class from Function in your code space. Are you doing anything funky with module loading contexts/vms?

For kicks, what happens if you change cursor.js:153 to:

    if (typeof callback != 'function') throw new Error("callback is not a function!")

@jamalex
Copy link
Author

jamalex commented Sep 8, 2011

Hi, thanks for looking into this! I tried changing that line, and still got the same error. It seems that "callback" is "undefined" at that point (according to console.log) -- and yeah, I wasn't doing anything funky with contexts, etc; I've copied the terminal session from clean install of mongolian (+ dependencies) through to running those 5 lines of code that give the error (see below). Strangely, if I pass "console.log" directly as the callback function, it works, but any combination of my own anonymous/non-anonymous functions (with 0 to 3 parameters) fails to work -- "callback" is undefined already as soon as it gets into "[object Object].findOne (./node_modules/mongolian/lib/collection.js)". Not sure if there's some other middleware hooking it and removing it before it gets there?

jamalex@wa:~/temp$ npm install mongolian

> [email protected] install /home/jamalex/temp/node_modules/mongolian/node_modules/mongodb
> bash ./install.sh

================================================================================
=                                                                              =
=  To install with C++ bson parser do <npm install mongodb --mongodb:native>   =
=  the parser only works for node 0.4.X or lower                               =
=                                                                              =
================================================================================
Not building native library for cygwin
[email protected] ./node_modules/mongolian 
├── [email protected]
├── [email protected]
└── [email protected]
jamalex@wa:~/temp$ node
> var Mongolian = require("mongolian")
> var server = new Mongolian
> mongo://localhost:27017: Connected
mongo://localhost:27017: Initialized as primary
mongo://localhost:27017: Connected to primary
Finished scanning... primary? mongo://localhost:27017

> var db = server.db("test")
> var docs = db.collection("docs")
> docs.findOne({}, function(err, doc) { console.log(doc) })
Error: callback is not a function!
    at [object Object].next (/home/jamalex/temp/node_modules/mongolian/lib/cursor.js:153:48)
    at [object Object].findOne (/home/jamalex/temp/node_modules/mongolian/lib/collection.js:53:42)
    at [object Context]:1:6
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)

@marcello3d
Copy link
Owner

I wonder if this is a limitation of running the REPL (it definitely does funky Context stuff). Does the same thing happen if you make a temp.js file and run node temp.js?

@jamalex
Copy link
Author

jamalex commented Sep 10, 2011

Interesting! It works if I run it non-interactively, as you predict, yes.

Does this happen for you too (in the REPL), or is it something peculiar about my configuration? I'm typically able to pass callbacks to functions in the REPL, so I'm curious about what's going on here.

Thanks for your work on this module, and let me know if I can help debug this in any way, as I like the approach you're taking, and would be happy to see this package mature.

@marcello3d
Copy link
Owner

It does happen for me, too. I did some digging and it is a result of the REPL instance of Function being different from Mongolian's. The fix above doesn't work because collections.findOne has a similar problem.

I can use typeof callback === 'function' everywhere, but that doesn't solve other instanceof problems (e.g. handling Date and RegExp).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants