Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize TypeError: phone.setUser is not a Function

I'm a little lost and would appreciate any help with this. I have a M:M association between my users table and phones table through my userPhones table.

user.js

  module.exports = (sequelize, DataTypes) => {
    const Users = sequelize.define('Users', {
    givenName: {
      type: DataTypes.STRING
    },
    sn: {
      type: DataTypes.STRING
    },
    mail: {
      type: DataTypes.STRING
    },
    title: {
      type: DataTypes.STRING
    },
    department: {
      type: DataTypes.STRING
    },
    sAMAccountName: {
      type: DataTypes.STRING,
      primaryKey: true
    },
    displayName: {
      type: DataTypes.STRING
    },
  }, {
    freezeTableName: true,
    timestamps: false

  });
  return Users; 
  };

phone.js

  module.exports = (sequelize, DataTypes) => {  
  const Phones = sequelize.define('Phones', {
    full_number: {
      type: DataTypes.STRING
    },
    telephone: {
      type: DataTypes.STRING
    },
    division_id: {
      type: DataTypes.FLOAT
   },
  }, {
    freezeTableName: true,
    timestamps: false

  });

  return Phones; 
  };

db.js

const Sequelize = require ('sequelize');

const sequelize = new Sequelize('db', 'user', 'password', {
    host: '127.0.0.1',
    dialect: 'mssql',
    pool: {
        max: 9,
        min: 0,
        idle: 10000
    }
});

const db = {};

db.Sequelize = Sequelize; 
db.sequelize = sequelize; 

//Gets models

db.users = require('./models/users.js')(sequelize,Sequelize);
db.phones = require('./models/phones.js')(sequelize,Sequelize);
db.userPhones = sequelize.define('userPhones')

//Sequelize Associations

db.users.belongsToMany(db.phones, {through: 'userPhones'});
db.phones.belongsToMany(db.users, {as: 'owners', through: 'userPhones'});

// Sync SQL with Sequelize Models USE CAUTION
sequelize.sync({alter:true});

module.exports = db;

phone post route for assigning a user to a phone

const express = require('express'),
  router = express.Router(),
  db = require('../db')
  ;


Phones = db.phones;
UserPhones = db.UserPhones;

// Single Phone User Assignment POST route

router.route('/:id/users')
  .post((req, res) => {
    let users = req.body.UserSAMAccountName;

    Phones.findById(req.params.id)
      .then(function (phone) {
        if (!phone) {
          res.status(404).json({ message: 'record not found!' })
        }
        phone.setUsers([users])
        .then(associatedUsers => {
          res.status(200).json({ message: `${associatedUsers} added!`});
        })

      })
      .catch(function (err) {
        res.status(500).json({ error: `${err}`});
      })

  });


module.exports = router;

I've previously been able to set assign phones to users and vice versa. I'm not exactly sure what has happened but the other day it just stopped working, and when I post to the API I am now getting:

TypeError: phone.setUsers is not a function

I've tried running sequelize.sync({force:true}); just to test and see if re-initializing everything would fix things but I haven't seem to get things back on track.

I've read about checking Phones.instance.prototypes but I'm not quite clear on how to go about finding that.

Update: I failed to mention that there isn't any issue running this from my user route to assign a phone to a user.

user POST route for assigning a phone to a user

const express = require('express'),
  router = express.Router(),
  db = require('../db')
  ;

  Users = db.users;
  UserPhones = db.UserPhones;

// Single User->Phone Assignment POST route

router.route('/:id/phones')
  .post((req, res) => {
    let phones = req.body.phoneID;

    Users.findById(req.params.id)
      .then(function (user) {
        if (!user) {
          res.status(404).json({ message: 'record not found!' })
        }
        user.setPhones([phones])
        .then(associatedPhones => {
          res.status(200).json({ message: `${associatedPhones} added!`});
        })

      })
      .catch(function (err) {
        res.status(500).json(err);
      })

  });

  module.exports = router;
like image 625
Derek Campanile Avatar asked Aug 23 '17 17:08

Derek Campanile


1 Answers

Try phone.setOwners or phone.setowners since you renamed your Users model in association to Phones to "owners".

like image 138
Justyn Nelson Avatar answered Oct 07 '22 19:10

Justyn Nelson