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 161 forks source link

Many to Many add method failing ( object is not a function ) #20

Closed thetutlage closed 9 years ago

thetutlage commented 10 years ago

I have 2 models Groups and Users as below :-

Groups.js

/**
* Groups.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {
     id:{
        type: 'integer',
        primaryKey: true,
        unique: true,
        autoIncrement: true
     },
     group_name:{
        type: 'string',
        required: true
     },
     group_description:{
        type: 'text'
     },
     group_status:{
        type: 'string',
        enum: ['Active','Inactive'],
        defaultsTo: 'Active'
     },
     thirdparty:{
      collection: 'thirdparty',
      via: 'groups'
     },
     users:{
        collection: 'users',
        via: 'groups'
     },
     tickets:{
        collection: 'tickets',
        via: 'groups',
      dominant: true
     }
  }
};

Users

module.exports = {

  attributes: {
     id:{
        type: 'integer',
        primaryKey: true,
        unique: true,
        autoIncrement: true
     },
     username: {
        required: true,
      type:'string',
      alpha: true,
        unique: true
     },
     email_address:{
        required: true,
      type: 'email',
        unique: true
     },
     password:{
      required: true,
      type: 'string'
     },
     alternate_email_address:{
        type: 'email'
     },
     contact_no:{
        type: 'string',
      numeric: true
     },
     account_status:{
        type: 'string',
        enum: ['Pending','Active','Inactive','Suspended'],
        defaultsTo: 'Pending'
     },
     is_verified:{
        type: 'boolean',
        defaultsTo: false
     },
     verification_token:{
        type: 'string'
     },
     groups:{
        collection: 'groups',
        via: 'users'
     }
  }
}

Inside my User controller , i want to associate the user i am going to create with an existing group

Groups.findOne(1).exec(function(err,group){
    if(err){
        return 
    }
    if(group){
        Users.create(/*somedata*/,function(err,success){
            if(success){
                group.users.add(success.id);
                group.save();
            }
        });
    }
});

And it is returning

TypeError: object is not a function
  at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:151:44
  at fn (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:78:5)
dmarcelino commented 9 years ago

Hi @thetutlage, are you still experiencing this issue? From looking at the code, are you sure success is an object with id?

For future reference, this sort of question will get more visibility and quicker response on the waterline main repo. This repo is meant for docs only.

thetutlage commented 9 years ago

Great i will keep that in mind and yes success has a key called id with valid type of object

devinivy commented 9 years ago

This certainly may be a bug. I think you'll have more luck if you pass a no-op to save, i.e.,

            if(success){
                group.users.add(success.id);
                group.save(function(){});
            }

Alternately, do any of your models have an empty/misdefined beforeUpdate lifecycle callback?