Sequelize TypeError:phone.setUser不是Function

时间:2017-08-23 17:15:34

标签: javascript node.js sequelize.js

我有点失落,并希望得到任何帮助。我通过<form action="@{/projectCentre/edit}" th:action="@{/projectCentre/edit}" th:object="${your_Model}" method="post"> <input type="hidden" id="orderField" name="orderField" th:value="${orderField}"/> <input type="hidden" id="orderType" name="orderType" th:value="${orderType}"/> <button name="action"></button> </form> 表在我的用户表和电话表之间建立了M:M关联。

user.js的

userPhones

phone.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; 
  };

db.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; 
  };
用于将用户分配到手机的

电话邮寄路线

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;

我以前能够为用户设置分配电话,反之亦然。我不确定发生了什么,但前几天它刚停止工作,当我发布到API时,我现在得到:

  

TypeError:phone.setUsers不是函数

我试过运行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; 只是为了测试,看看重新初始化一切是否会解决问题,但我似乎没有让事情回到正轨。

我读过关于检查sequelize.sync({force:true});的内容,但我不清楚如何找到它。

更新:我没有提到从我的用户路线运行此操作以将电话分配给用户没有任何问题。

用于将电话分配给用户的

用户POST路由

Phones.instance.prototypes

1 个答案:

答案 0 :(得分:1)

尝试meta-dynamicsphone.setOwners,因为您将与手机相关联的用户模型重命名为“所有者”。