efkan
efkan

Reputation: 13227

I can't establish a simple association on Sails.js (relation model's field shows undefined)

I've been trying a simple 'one way association' on v0.9.16 and later v.10.0.rc8. I'm guessing, I'm doing something wrong. I have two collections on my MongoDB and I have two models and a controller such as following;

my User.js file;

module.exports = {

  connection: 'mongo',

  attributes: {

    username: {
        type: 'string',
        required: true
    },

    encryptedPassword: {
        type: 'string' , 
        required: true
    },

    //one way association
    personel: {
      model: 'Personel'
    }  

  }
};

my Personel.js file;

module.exports = {

  connection: 'mongo',

  attributes: {
      name: 'string',
      phone: {
          mobile: 'string',
          office: 'string'
      },
      email: 'string',
      gender: 'string'
      department: {
          department_id: 'integer',
          department_name: 'string'
      },
      title: {
          title_id: 'integer',
          title_name: 'string'
      }
  }
};

my UserController.js file;

module.exports = {
show : function (req, res) {
User
.findOne(req.param('id'))
.populate('personel')
.exec(function(err, user) {
console.log(user);
});

}

Console writes 'undefined' for personel field;

{ username: 'jkidd',
personel_id: '1',
personel: undefined }

Edit

To provide association I've just added following codes to my User model

//one way association
personel: {
  model: 'Personel'
}  

and I'm trying to

show : function (req, res) {
   User
    .findOne(req.param('id'))
    .populate('personel')
    .exec(function(err, user) {
      if(err) return res.json(err, 400);
      if(!user) return res.json({ error: 'No User with that Id' }, 404);

      console.log(user);

      res.view({
        user : user
      });
    });            
}

Upvotes: 0

Views: 2945

Answers (2)

mtjburton
mtjburton

Reputation: 11

I wanted to add a comment but cannot as my rep isn't high enough. As already stated. You missed the Association in your Personel.js.

With this in place waterline should link the models up but it isn't for some reason. I'd suggest removing your id attributes as these are likely confusing matters as you'll get _id added automatically for you which is what waterline will use for the associations.

In your code where you associate the personnel to user do so by setting user.personnel = personnel.id.

If you require more help can you post the code/commands you're using to associate the models too.

[EDIT]

With regards to nested attributes check Sails.js - how to update nested model

Your User model should be as follows:

module.exports = {

    attributes: {

        username: {
            type: 'string',
            required: true
        },

        encryptedPassword: {
            type: 'string' ,
            required: true
        },

        //one way association
        personel: {
            collection: 'personel',
            via: 'user',
        },

    },
};

And your personel model:

module.exports = {

    attributes: {

        name: 'string',

        email: 'string',

        gender: 'string',

        user: {
            model: 'user',
        },

    },

};

And just a little modification to your controller so that you can view the JSON output in browser:

module.exports = {

    show: function (req, res) {
        User2.find()
        .populate('personel')
        .exec(function(err, user) {
            console.log(user);
            return res.send(200, user);
        });

    },

}

[EDIT]

I've just realised you weren't talking about a one to many association, the above is that, not a one to one.

Your edited code looks find for the one to one join. How are you creating the models? I think that's your issue, you need something like:

Personel.create({ name: 'personel'}, function(err, createdPersonel) {
    User.create({ name: 'user', personel: createdPersonel.id }, function(err, createdUser) {
        res.send(200, createdUser )
    }
}

That's without error checking and assuming you're in a controller.

Upvotes: 0

mdunisch
mdunisch

Reputation: 3697

You missed the Association in your Personel.js:

module.exports = {
  connection: 'mongo',
  attributes: {
    department_id: 'integer',
    department_name: 'string',
    owner: {
      model: "User"
    }
  },

 }

See for more Info: http://beta.sailsjs.org/#/documentation/reference/Models/Associations/OnetoOne.html

Hint: You dont need to config the _id - waterline handle that for you!

Edit:

Also a Issue

And you cannot use "nested attributes" (see: How to translate models in Sails.js?). Change that also!

edit: you almost there... ;-)

module.exports = {

attributes: {

    username: {
        type: 'string',
        required: true
    },

    encryptedPassword: {
        type: 'string' ,
        required: true
    },

    //one way association
    personel: {
        model: 'user'
    },

},
};

and your personel.js

module.exports = {

attributes: {

    name: 'string',

    email: 'string',

    gender: 'string',

    owner: {
        model: 'user',
    },

},

};

Upvotes: 1

Related Questions