1

In sails-mongo(0.10.0-rc3/beta) it looks like the foreign id is converted to ObjectID so if I have a two models with one to one relationship how do I query using an id?

Example:

ModelA
attributes: {
 user: { model: 'User' }
}

User.js
attributes: {
  ...
}

Now I want to query do like ModelA.find(user: req.session.user).exec(console.log) and the log returns null [] instead of the all the items in ModelA that belongs to the current user.

Edit: I tried converting req.session.user again to ObjectID using the mongodb module new ObjectID(req.session.user) and everything works. I'm not sure why it didn't work the last time.

Thanks

ginad
  • 1,863
  • 2
  • 27
  • 42
  • Have you verified that `req.session.user` contains a valid user ID? – sgress454 Apr 11 '14 at 20:54
  • Yes, I printed out req.session.user and it returned the correct user id, when I do `typeof req.session.id` I get sting so I'm wondering maybe I should convert it to ObjectID but still no result is being returned. – ginad Apr 13 '14 at 00:14

2 Answers2

0

In sails-redis@0.10.0-rc3 querying associated models seems to behave as expected.

First, I created a basic User model:

module.exports = {
    attributes: {
        name: 'string'
    }
};

Second the ModelA model with the associated User:

module.exports = {
    attributes: {
        user: { model: 'User' }
    }
};

Then I created a small mocha test for checking the association query and reducing any errors introduced somewhere else (e.g. in your session handling):

var expect = require('chai').expect,
    sails = require('sails'),
    async = require('async');

describe('sails-mongo associations', function() {

    before(function (done) {
        sails.lift(function (err, sails) {
            done(err);
        });
    });

    after(function (done) {
        sails.lower(function (err) {
            done(err);
        });
    });

    beforeEach(function(done) {
        async.auto({
            deleteUsers: function(done) {
                User.destroy(done);
            },
            deleteModelA: function(done) {
                ModelA.destroy(done);
            }
        }, function(err) {
            done(err);
        });
    });

    describe("#find", function() {
        it("should allow to find a record using an associated model", function(done) {
            User.create({ name: 'Test User' }).exec(function(err, user) {
                expect(err).to.be.null;
                expect(user).to.contain.key('id');

                ModelA.create({ user: user.id }).exec(function(err, model) {
                    expect(err).to.be.null;
                    expect(model).to.contain.keys('id', 'user');

                    ModelA.find({ user: user }).populate('user').exec(function(err, model) {
                        expect(err).to.be.null;
                        expect(model).not.to.be.null.and.contain.key('user', user);
                        done();
                    });
                }); 
            });
        });

    });
});

The mocha test completes successfully without any error. Which probably means (as Scott Gress already assumed) your req.session.user variable does not contain the value you expect. Internal conversion to mongos ObjectID is working.

Cheers, David

david.schreiber
  • 3,851
  • 2
  • 28
  • 46
  • I've tried this and I get and empty array `[]` and the model.user is `undefined`. I've printed the user and it's returning the correct user data. I also tried `ModelA.find({user: user.id})` instead of `{user: user}`but still no result returning. – ginad Apr 21 '14 at 04:13
  • Does your ModelA document contain any data at all? To be sure that your database is in the correct state, you could try fetching the whole document and look for the data manually. – david.schreiber Apr 22 '14 at 09:28
  • Yes I checked both the user and modela table and there are contents. and all contents are connected to a user and same with the user. – ginad Apr 23 '14 at 08:09
0

I had the same problem myself. It seems sails-mongo wouldn't convert your string id's to ObjectId()-syntax unless it was a primary key (_id).

Luckily, this is fixed in a recent commit

Update your package.json to use the master-branch, or wait for an rc4-release.

stensrud
  • 156
  • 5