geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

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

Closed acivang closed 9 years ago

acivang commented 9 years ago
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 commented 9 years ago

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 commented 9 years ago

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

acivang commented 9 years ago

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 commented 9 years ago

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!