co-monk icon indicating copy to clipboard operation
co-monk copied to clipboard

"TypeError: object is not a function" when using co with co-monk

Open egeland opened this issue 10 years ago • 2 comments

Hi, I'm still a JS newbie, and very much a n00b to generators, co, monk and co-monk. Probably, I'm making a silly error, but my Google-fu is failing me, so I'm hoping for a pointer in the right direction..

I get a "TypeError: object is not a function" when trying the example in https://github.com/tj/co-monk/tree/67a867016c945ea9cb6cafdc8b5c97788b797ac5 (using co so that yield won't get an error):

})();
  ^
TypeError: object is not a function

Here's my code:

"use strict"; /* jshint node: true, esnext: true */

var monk = require('monk'),
    wrap = require('co-monk'),
    co   = require('co'),
    db   = monk('localhost:27017/test');

var users = wrap(db.get('users'));

co(function *(){
    yield users.remove({});
    // the rest of the code from above
    // ... 
    // finally end generator function
    // and execute co
})();

What am I doing wrong?

Running node v4.0.0, and get the same error with or without --harmony option.

egeland avatar Sep 16 '15 23:09 egeland

So I read the source code of co and found co.wrap() and thought "hmm.. let's see if that helps"..

It did - this code works:

"use strict"; /* jshint node: true, esnext: true */

let monk = require('monk'),
    wrap = require('co-monk'),
    co   = require('co'),
    db   = monk('localhost:27017/test');

let users = wrap(db.get('users'));

co.wrap(function *(){
    yield users.remove({});
    yield users.insert({name: 'Test User'});
    var res = yield users.find({});
    console.log(res);
})()
    .then(function () {
        console.log("All done");
        process.exit();
    });

egeland avatar Sep 16 '15 23:09 egeland

It did and this code works : for me also, been stuck for more than 9 hours, many thanks

suhail339 avatar Apr 21 '17 12:04 suhail339