0

I have two collections; user and address. Address collection has association with user collection.

    module.exports = {
    attributes: {
    email: {
    type: 'email',
    required: true,
    unique: true
    },
    cart: {
        collection: 'cart',
        via: 'user'
    },
    isAdmin : {
        type : 'boolean',
        defaultsTo : false
    },
 }

module.exports = {
attributes: {
    user: {
        model: 'user',
        required: true
    },
    address: {
        type: 'string',
    },
    addressAdditional: {
        type: 'string',
    },
    city: {
        type: 'string',
    },
    state: {
        type: 'json',
    },
    zip: {
        type: 'string',
    },
}

I have the userId and address info passed from the UI and I would like to update the address collection. I tried the following snippet but it does not seem like the data is getting updated.

var address = {
  address: "123",
  city: "Test City",
  state: "NY",
  zip: "12345"
};

User.findOne({user:userId}).then(function(model){
Address.update({id:model.id}, address)});

What am I doing wrong? Thanks

gamegeek24
  • 23
  • 6
  • Is the field 'id' of a document in the address collection an 'ObjectId' type? If so, convert this id to an 'ObjectId'. – Kevin Jun 21 '16 at 14:10

1 Answers1

0

After looking in to the sails adapter.js javascript, a function called mongo.objectId to create an ObjectId.

 var addressObjectId = mongo.objectId(model.id);
 Address.update({_id:addressObjectId}, address)})

https://github.com/balderdashy/sails-mongo/blob/master/lib/adapter.js#L266

Kevin
  • 506
  • 6
  • 13
  • I couldn't figure out how to use adapter.js, instead I took the approach provided in http://stackoverflow.com/questions/25699097/native-update-in-sails-with-mongo-does-not-work-with-objectid – gamegeek24 Jun 22 '16 at 01:35