gpt4 book ai didi

javascript - AuthenticationController 总是抛出错误,TypeError : Cannot read property 'create' of undefined

转载 作者:行者123 更新时间:2023-12-03 22:34:24 27 4
gpt4 key购买 nike

我对与 sequelize 和 sqlite 一起使用的 Authenticati onController 有问题
当我使用 postman 测试 POST 请求时,它总是给出状态 400 的响应
{
错误:'出了点问题'
}
enter image description here
这是日志
::1 - - [21/Jul/2020:15:40:33 +0000] "POST/register HTTP/1.1"400 30 "-""PostmanRuntime/7.26.1"
enter image description here
这是AuthenticationController的代码

const {User} = require('../models')
module.exports = {
async register(req, res){
try {
const user = await User.create(req.body)
res.send(user.toJSON())
} catch (error) {
console.log(error)
res.status(400).send({
error: 'Something is wrong'
})
}
}
}

用户型号代码

module.exports = (sequelize, DataTypes) =>
sequelize.define('User', {
email: {
type: DataTypes.STRING,
unique: true
},
password: DataTypes.STRING
})

型号索引代码

const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}

const sequelize = new Sequelize(
config.db.database,
config.db.user,
config.db.password,
config.db.options
)
fs
.readdirSync(__dirname)
.filter((file)=>{
file != 'index.js'
})
.forEach((file)=>{
const model = require(path.join(__dirname, file))(
sequelize,
Sequelize.DataTypes
);
// const model = sequelize.import(path.join(__dirname,file))
db[model.name] = model
})
db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

路线代码

const AuthenticationController = require('./controllers/AuthenticationController');

module.exports = (app) => {
app.post('/register',
AuthenticationController.register)
}

早些时候,它抛出了一个错误“类型错误:无法读取未定义的属性‘创建’”
但是当我重新启动服务器时,问题就解决了。但是我不知道为什么 try 块会失败。
任何人都可以对此有所了解吗?谢谢

最佳答案

最后,谢天谢地,它被修复了!
这是/models/index.js 的修订代码

const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}

const sequelize = new Sequelize(
config.db.database,
config.db.user,
config.db.password,
config.db.options
)
fs.readdirSync(__dirname)
// .filter((file) => {
// file != "index.js";
// })
.filter(
(file) =>
file.indexOf(".") !== 0 && file !== "index.js" && file.slice(-3) === ".js"
)
.forEach((file) => {
const model = require(path.join(__dirname, file))(
sequelize,
Sequelize.DataTypes
);
// const model = sequelize.import(path.join(__dirname,file))
db[model.name] = model;
});
db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

过滤器块已更新如修订所示,在链接的帮助下
https://www.codota.com/code/javascript/functions/sequelize/Sequelize/import
改变的两件事是
  • 将 'sequelize.import' 替换为 'require'
  • 更新了“过滤器”块,如图所示。

  • 这是结果
    enter image description here
    感谢 crumkev 的洞察力,让我找到了答案。我希望这对可能遇到类似问题的其他人有所帮助。

    关于javascript - AuthenticationController 总是抛出错误,TypeError : Cannot read property 'create' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63018343/

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