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

TypeError: Cannot call method 'collection' of undefined. #252

Closed
acivang opened this issue Mar 3, 2015 · 4 comments
Closed

TypeError: Cannot call method 'collection' of undefined. #252

acivang opened this issue Mar 3, 2015 · 4 comments

Comments

@acivang
Copy link

acivang commented Mar 3, 2015

var model = require('model');

var Foo = function () {
    this.setAdapter('mongo', {
        "hostname":"localhost",
        "port":27017,
        "username":"",
        "password":"",
        "dbName":"itemDB"
    });

    this.defineProperties({
        name: { type: 'string', required: true }
    });
};

Foo = model.register('Foo', Foo);

var foo = Foo.create({"name":"aci test"});

foo.save();

when I run the code,it throw

TypeError: Cannot call method 'collection' of undefined.

throw code was:

collection = this.client._db.collection(collectionName)

the _db was null
I debug model's code,it didn't execute connect function in adapters/mongo/index.js

and my mongodb module version is 2.0.18,and model module version is 6.0.1.
How can i fix it?
Thx!

@mde
Copy link
Contributor

mde commented Mar 3, 2015

Sorry that it's so hard to bootstrap Model all by itself. You can take a look at the Mongo adapter tests to see one way to get the adapter set up and working: https://github.com/geddy/model/blob/master/test/integration/adapters/mongo/index.js#L16

@acivang
Copy link
Author

acivang commented Mar 4, 2015

@mde thank u very much,i should see it,i will comment it later!

@acivang
Copy link
Author

acivang commented Mar 4, 2015

hello @mde !
I written code like that test flow:

var model = require('model');

var Foo = function () {
    this.setAdapter('mongo', {
        dbname:"itemDB"
    });

    this.defineProperties({
        name: { type: 'string', required: true }
    });
};

Foo = model.register('Foo', Foo);

var foo = Foo.create({"name":"aci test 2015"});
var adapter = Foo.getAdapter();
adapter.connect(function(){
    foo.save();
});

it was worked! but,was it a bug?or was my code's error?

@mde
Copy link
Contributor

mde commented Mar 4, 2015

I think you were missing the step for actually creating and connecting the adapter in your previous code. I'm going to go ahead and close this issue -- if you continue to have problems, please feel free to reopen it!

@mde mde closed this as completed Mar 4, 2015
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