balderdashy / waterline-docs

WARNING: The content in this repo is out of date! See https://github.com/balderdashy/sails-docs for the most up-to-date documentation
452 stars 163 forks source link

The save method throw a error, in many to many cases. #136

Closed Maxtermax closed 7 years ago

Maxtermax commented 7 years ago

I am using mongodb with mongo-sails adapter, and sails version 0.12

// \api\models\User.js

module.exports =  {
  autoPK: false,
  schema: true,
  addEvent: function(data) {
    return (new Promise((resolve, reject)=> {
        User.findOne(data.id)
            .then(function(docs) {
              if(!docs) return reject(new Error("notFound"));
             //eid = event id 
              docs.events.add(data.eid);
              docs.save((err)=> {
                console.log(err, "err");
                if(err) return reject(new Error("serverError"));
                return resolve(docs);
              });
            })
            .catch(reject);
      })
    )
  },//end addEvent
  attributes: {
      id: {
       type: 'objectid',
       primaryKey: true
    },
    isLogin: {
        type: 'boolean',
        defaultsTo: false
    },
    password: {
      type: 'string',
      size: 20,
      required: true
    },
   name: {
    type: 'string',
    required: true
   },
   email: {
    type: 'string',
    required: true,
    unique: true
   },

    events: {
      collection: 'event',
      via: 'speakers',
      dominant: true
    }
};
// \api\models\Event.js
module.exports = {
  autoPK: false,
  schema: true,
  attributes: {
    id: {
      type: 'objectid',
      primaryKey: true
    },
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string'
    },
    startAt: {
      type: 'date',
      required: true
    },
    endAt: {
      type: 'date',
      required: true
    },
    eventType : {
      type: 'string',
      enum: ['presential', 'virtual'],
      required: true
    },
    location: {
      type: 'string'
    },
    speakers: {
      collection: 'User',
      via: 'events'
    }
  }
};

Issue

When i call the method addEvent of the model User, not matter what i do always return this error: Invalid attributes sent to User: • id • [object Object]

Somehow the save method try to update the model, and reset the id by an object and then trigger the validation that throw this error.