gpt4 book ai didi

javascript - Sequelize TypeError : phone. setUser 不是函数

转载 作者:太空宇宙 更新时间:2023-11-03 22:09:48 24 4
gpt4 key购买 nike

我有点迷失,希望得到任何帮助。我通过 userPhones 表在用户表和电话表之间建立了 M:M 关联。

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;

用于将用户分配到电话的电话发布路由

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;

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

TypeError: phone.setUsers is not a function

我尝试运行 sequelize.sync({force:true}); 只是为了测试并看看重新初始化所有内容是否可以解决问题,但我似乎还没有恢复正常追踪。

我读过有关检查 Phones.instance.prototypes 的内容,但我不太清楚如何找到它。

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

用于将电话分配给用户的用户 POST 路由

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;

最佳答案

尝试 phone.setOwnersphone.setowners,因为您将与电话关联的用户模型重命名为“owners”。

关于javascript - Sequelize TypeError : phone. setUser 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com