Sails.js - how to update a 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 am currently registering a user and then updating profile information after registration. How can I add profile data to this model?

I read elsewhere that the push method should work, but that is not the case. I get this error: TypeError: Object [object Object] does not have a 'push' method

        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)
            });

        });
+4
source share
3 answers

@ Zolmeister is right. Sails only supports the following types of model attributes

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 No. 124 .

You can get around this bypassing sails and using your own Mongo methods:

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

    });

});

, - . , ( ObjectIds, pubsub ..).

+4

Sails ( ). 'json'. :

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

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

It is too late to answer, but for others (as a link) they can do something like this:

Users.findOne(req.session.user.id).done(function(error, user) {
  profile = {
            firstname : first,
            lastname : last,
            zipcode: zip
      };
  User.update({ id: req.session.user.id }, { profile: profile},         
        function(err, resUser) {
  });           
});
+1
source

All Articles