0

I'm trying to do the following:

// Task Model

module.exports = {
 schema: true,             
 attributes: {

    directProy: {
        type: 'string',
        required: true
    },
    user: {
        type: 'string',
        required: true
    },
    checkUser: {
        type: 'boolean',
        defaultsTo: false
    },
    proy: {
        type: 'string',
        required: true
    },
    pieza: {
        type: 'string',
        required: true
    },
    hours: {
        type: 'string',
        required: true
    },
    obs: {
        type: 'text',
        defaultsTo: "lorem ipsum"
    },
    check: {
        type: 'boolean',
        defaultsTo: false
    },
    userName: function() {
        User.findOne(this.user).done(function(err, user){
            if(err){ return err;}
            return user.name;
        });
    }
}

};

In the method "userName" I'm trying to get the name of a user with the ID it stored in the "user" attribute.

but when I run the "username" method, brings me back "undefined", I think this has to be a problem of asynchronous type

Would greatly appreciate the help they can give me since I have no idea how to associate values ​​between models and this is very useful

Cristian G
  • 779
  • 2
  • 10
  • 21
  • @tape's answer below is correct, but for your case--and if you're using Sails v0.10.x--you should really just use model associations and make a one-to-many association between User and Task. See docs [here](http://beta.sailsjs.org/#/documentation/reference/Models/Associations/OnetoMany.html). – sgress454 Jun 19 '14 at 22:39

2 Answers2

1

try passing a callback.

userName: function(cb) {
    User.findOne(this.user).done(function(err, user){
       cb(err, user.name);
    });
}

Then when you are calling it, make sure to pass a callback.

model.userName(function(err, username) {
   console.log(username);
});
tpae
  • 6,286
  • 2
  • 37
  • 64
0

your should use .exec instead of .done since it will not be avalible in sails@0.10 http://beta.sailsjs.org/#/documentation/reference/Models

yellowsir
  • 741
  • 1
  • 9
  • 27