question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

user.getAssociations not working

See original GitHub issue

I have a User and a Trip model. Users belong to many Trips and Trips belong to many Users (both through the UserTrip table). I’m using v3.13.0 with postgres.

I can call trip.getUsers(), but when I try user.getTrips() I get the following error

Unhandled rejection TypeError: undefined is not a function

Models

User

"use strict";

module.exports = function(sequelize, DataTypes) {

  var User = sequelize.passportLocalSequelize.defineUser(sequelize, {
    // login stuff
      email:      DataTypes.STRING(254),
      hash:       DataTypes.TEXT,
      salt:       DataTypes.STRING(255),

    // personal info
      first_name: DataTypes.STRING,
      last_name:  DataTypes.STRING,
      phone:      DataTypes.STRING(12), //length needed for international numbers
      address:    DataTypes.STRING,
      city:       DataTypes.STRING,
      state:      DataTypes.STRING,
      country:    DataTypes.STRING,
      zip:        DataTypes.STRING,
      birthday:   DataTypes.DATEONLY,

    // app shit
      customerId:DataTypes.STRING(2000),
      role: DataTypes.STRING,
      credits: DataTypes.INTEGER,
      referral_id: DataTypes.STRING,
      referral_code: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        User.belongsToMany(models.Trip, {through: 'UserTrip'})
      }
    }
  });
return User;
};

Trip

"use strict";
module.exports = function(sequelize, DataTypes) {
  var Trip = sequelize.define("Trip", {
    // waypoint info
      start_address:        DataTypes.TEXT,
      start_point:          DataTypes.STRING,

      destination_address:  DataTypes.TEXT,
      destination_point:    DataTypes.STRING,

      departure_time:       DataTypes.DATE,
      return_time:          DataTypes.DATE,

    // options
      is_private:           DataTypes.BOOLEAN,
      ride_type:            DataTypes.TEXT,
      max_passenger:        DataTypes.INTEGER,
      food_drink:           DataTypes.BOOLEAN,

    // non-visible
      passenger_count:      DataTypes.INTEGER,
      estimated_cost:       DataTypes.DECIMAL(10,2),
      actual_cost:          DataTypes.DECIMAL(10,2),
      is_completed:         DataTypes.BOOLEAN,
      organizer_id:         DataTypes.INTEGER
  }, {
    classMethods: {
      associate: function(models) {
        Trip.belongsToMany(models.User, {through: 'UserTrip'});
        Trip.belongsTo(models.Event);
      }
    }
  });
  return Trip;
};

I can’t for the life of me figure out what I’m doing wrong. I would massively appreciate any insight you might have– Let me know if there’s anything I’ve left out that would help solve the issue

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
duncangrahamcommented, Nov 18, 2015

The only time associate gets called is in my models/index.js file

Object.keys(db).forEach(function(modelName) {
  if ("associate" in db[modelName]) {
    db[modelName].associate(db);
  }
});
0reactions
dscarlincommented, May 23, 2019

I am having a similar issue, none of the association methods are working for me. I am new to Sequelize.

module.exports = (sequelize, DataTypes) => {
    let User = sequelize.define("User", {
      user_name: {
        type: DataTypes.STRING,
        allowNull: false, 
        unique: true,
        validate: {
          notEmpty: true,
        }
      },
      email: {
        type: DataTypes.STRING, 
        allowNull: false,
        unique: true, 
          validate: {
            isEmail: true
          }
      },
      phone_number: {
        type: DataTypes.STRING, 
        allowNull: false,
          validate: {
            not: ['[a-z]','i']
          }
      },
      status: {
        type: DataTypes.BOOLEAN, 
        defaultValue: false,
          validate: {
            isBoolean:function (val) {
              return (typeof(val)=='boolean')
                 }
          }
      },
      text_enabled: {
        type: DataTypes.BOOLEAN, 
        defaultValue: false,
          validate: {
            isBoolean:function (val) {
              return (typeof(val)=='boolean')
            }
          }
      } 
    });

    User.associate = (models) => {
      User.belongsToMany(models.Group, { through: 'UserGroup' } );
     
    };  
    
    return User;
}
module.exports = (sequelize, DataTypes) => {
    let Group = sequelize.define("Group", {
      name: {
        type: DataTypes.STRING,
        allowNull: false, 
        validate: {
          notEmpty: true,
        }
      }
    });
    Group.associate = (models) => {
      Group.belongsToMany(models.User, { through: 'UserGroup'});
     
    };  
    return Group;
  }

I console logged inside the if statement in index.js and they are both being associated. They both have all the methods listed in the prototype chain. If I call them with db.User.getGroups() I get “is not a function” if i call db.User.prototype.getGroups() I get "Executing (default): SHOW INDEX FROM ‘User Group’ FROM ‘My Database Name’ and then “Unhandled rejection TypeError"Cannot read property ‘id’ of undefined”

Any help would be appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UserPrincipal.GetGroups fails with unknown error
I have the issue you describe. I have the problem getting groups when user DN contains "/". Can you tell me what's the...
Read more >
PK51257: ENABLE HONORING USER OR GROUP FILTER ...
When LDAP is used as user registry, in getGroups and getUsers when a full DN is used, the code searches for the DN...
Read more >
Flickr Services: Flickr API: flickr.people.getGroups
flickr.people.getGroups. Returns the list of groups a user is a member of. ... 1: User not found: The user id passed did not...
Read more >
Users in User Group Not Show as Community Members
When we add users to a group - we can call it MyCommunityUsers and then add that group to a community, the portlets...
Read more >
User.Principal.GetGroups() lookup wrong DC - Microsoft Q&A
I work for a customer whose program makes a query on the groups of an AD. Until the time the DC was changed,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found