Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js - how to update nested model

attributes: {
    username: {
        type: 'email', // validated by the ORM
        required: true
    },
    password: {
        type: 'string',
        required: true
    },
    profile: {
        firstname: 'string',
        lastname: 'string',
        photo: 'string',
        birthdate: 'date',
        zipcode: 'integer'
    },
    followers: 'array',
    followees: 'array',
    blocked: 'array'
}

I currently register the user then update profile information post-registration. How to I go about adding the profile data to this model?

I read elsewhere that the push method should work, but it doesn't. I get this error: TypeError: Object [object Object] has no method 'push'

        Users.findOne(req.session.user.id).done(function(error, user) {

            user.profile.push({
                firstname : first,
                lastname : last,
                zipcode: zip
            })

            user.save(function(error) {
                console.log(error)
            });

        });
like image 729
diskodave Avatar asked Mar 21 '23 18:03

diskodave


1 Answers

@Zolmeister is correct. Sails only supports the following model attribute types

string, text, integer, float, date, time, datetime, boolean, binary, array, json

They also do not support associations (which would otherwise be useful in this case)

GitHub Issue #124.

You can get around this by bypassing sails and using mongo's native methods like such:

Model.native(function(err, collection){

    // Handle Errors

    collection.find({'query': 'here'}).done(function(error, docs) {

        // Handle Errors

        // Do mongo-y things to your docs here

    });

});

Keep in mind that their shims are there for a reason. Bypassing them will remove some of the functionality that is otherwise handled behind the scenes (translating id queries to ObjectIds, sending pubsub messages via socket, etc.)

like image 153
colbydauph Avatar answered Apr 06 '23 06:04

colbydauph