1

So I've been at this for awhile and can't see how my code is different from the documentation. I've also checked out this question, this question, this question, and this unanswered different question.

For my admin panel I'm trying to query to get all the information associated with a user and display a 'master' user profile to the admin.

My User model looks like this:

module.exports = {
    autoPK: true,
    attributes : {
        id: {
            type: 'integer',
            primaryKey: true,
            unique: true
        },
        email : {
            type : 'email',
            unique : true,
            required : true,
        },
        password : {
            type : 'string',
            minLength : 8,
            required : true
        },
        admin:{
            type: 'bool'
        },
        user_profile:{
            collection: 'userprofile',
            via: 'user_id',
        },
        properties: {
            collection: 'subjectproperties',
            via: 'user_id'
        },
        employment_info: {
            collection: 'employmentinfo',
            via: 'user_id'
        },
        file_uploads: {
            collection: 'fileupload',
            via: 'user_id'
        },
        nearest_living_relatives:{
            collection: 'nearestlivingrelative',
            via: 'user_id'
        },
        mortgage_info: {
            collection: 'mortgageinfo',
            via: 'user_id'
        },
        user_progression_state:{
            collection: 'userprogressionstate',
            via: 'user_id'
        },
        users_applied_loan_values:{
            collection: 'usersappliedloanvalues',
            via: 'user_id'
        }
    }
}

I don't want to list out all the belongs to user models cause there are a lot of them, but here is one of the simpler one's.

EmploymentInfo.js

module.exports = {
    tableName: "employment_info",
    attributes : {
        employers_name:{
            type: 'string',
            required: true
        },
        employers_address:{
            type: 'string',
            required: true
        },
        employers_city:{
            type: 'string',
            required: true
        },
        employers_state:{
            type: 'string',
            required: true
        },
        employers_zip:{
            type: 'string',
            required: true
        },
        job_position:{
            type: 'string',
            required: true
        },
        years_in_position:{
            type: 'string',
            required: true
        },
        years_in_industry:{
            type: 'integer',
            required: true
        },
        user_id:{
            model:'user'
        }
    }
};

And as for my controller:

    create_admin_user_profile: function(req, res){
        var user_id = req.query.userId;
        User.find({'id': user_id}).populateAll().exec(function(err, user){
            if(err || user.length === 0){
                sails.log.verbose(err); 
            }else{
                sails.log.verbose(user);
            }
        });
    },

It doesn't error out but all I see in the terminal is this for the above:

[ { user_profile: [],
properties: [],
employment_info: [],
file_uploads: [],
nearest_living_relatives: [],
mortgage_info: [],
user_progression_state: [],
users_applied_loan_values: [],
id: 5,
email: 'test@test.com',
admin: 1 } ]

Even though there is an entry in all of those tables for that user.

If I change the line:

 User.find({'id': user_id}).populateAll().exec(function(err, user){

To:

 User.find({'id': user_id}).populate('employment_info').exec(function(err, user){

Same but shorter result:

[ { employment_info: [],
id: 5,
email: 'test@test.com',
admin: 1 } ]

I've tried changing the case, I've tried adding columnName to the user_id attribute, I've tried changing the column name across the entire breadth of the project to not have an under_score in it, though that never seemed to be issue in it picking up the names correctly, but nothing I've done seems to work. I've also tried uninstalling sails, and the sails-mysql adapter and clearing my npm cache.

At this point my just stuck, I really can't see a reason why it's not working.

As for project info:
Sails v: 0.12.11
npm v: 3.10.9
node v: 7.2.0


Additional info asked for in comments:

SQL row taken right from db for user 5

employers_name, employers_address, employers_city, employers_state, employers_zip, job_position, years_in_position, years_in_industry, user_id
'Company',      'Mill Steet',      'SLC',          'Utah',          '88888',       'Developer',  '2',               '2',               '5'

And json format returned by find method in EmploymentInfo.js controller

{
    "employmentInfo": {
        "employers_name": "Company",
        "employers_address": "Mill Steet",
        "employers_city": "SLC",
        "employers_state": "Utah",
        "employers_zip": "88888",
        "job_position": "Developer",
        "years_in_position": "2",
        "years_in_industry": 2,
        "user": 5
    }
}

The reason the last param is user and not user_id is because I rename it in the find method to serve the front-end mvc which also has the ability to work with associations. It's also why the JSON has the format it does.

Code from the find method that replaces user_id:

EmploymentInfo.find({'user_id': user_id}).exec(function(err, profile){
        if(err || !profile.length){
            return res.json(err);
        }else{
            res.status(200);
            profile[0].user = profile[0].user_id;
            delete profile[0].user_id;
            res.send({'employmentInfo': profile[0]});
        }
});

However I've tried not renaming it; I've also tried getting rid of my find override and just relying on the blueprint find method, neither of those worked either.

Community
  • 1
  • 1
Ryan
  • 5,644
  • 3
  • 38
  • 66

0 Answers0