gpt4 book ai didi

sequelize.js - Sequelize : Column not found immediately after creation

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

在同一个发布 route ,我试图创建一个新类(class),然后通过 findAll() 方法选择该类(class)。我这样做是因为我想将位置 header 设置为 jsut 创建的类(class)的正确 URI。为此,我需要访问其自动生成的 ID。问题是,当我调用 findAll() 方法时,它说找不到我刚刚创建的类(class)。

app.post('/api/courses', async (req, res) => {
try {

const newCourse = req.body;


await Course.create({
title: newCourse.title,
description: newCourse.description,
estimatedTime: newCourse.estimatedTime,
materialsNeeded: newCourse.materialsNeeded,
userId: 1

})
const course = await Course.findAll({
attributes: [newCourse.title]
})


res.location('/api/courses/' + course.id)
res.status(201);
}catch(error) {
console.warn(error);
}

})

app.get('/api/courses/:id', async (req, res) => {
try {
const courseRequest = req.params.id;

const course = await Course.findAll({
where: {
id: courseRequest
},
attributes: ['title', 'userId']
})

res.json({
course
});
}catch(error) {
console.error(error);
}

})

Naor Levi 发表评论后更新
我将新创建的类(class)记录到控制台,它返回以下内容:
Course {
dataValues:
{ id: 11,
title: 'Investing for beginners',
description: 'Learn to invest your money wisely',
estimatedTime: '3 weeks',
materialsNeeded: 'pen and paper',
userId: 1,
updatedAt: 2020-01-13T23:50:53.748Z,
createdAt: 2020-01-13T23:50:53.748Z },
_previousDataValues:
{ title: 'Investing for beginners',
description: 'Learn to invest your money wisely',
estimatedTime: '3 weeks',
materialsNeeded: 'pen and paper',
userId: 1,
id: 11,
createdAt: 2020-01-13T23:50:53.748Z,
updatedAt: 2020-01-13T23:50:53.748Z },
_changed:
{ title: false,
description: false,
estimatedTime: false,
materialsNeeded: false,
userId: false,
id: false,
createdAt: false,
updatedAt: false },
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'Courses', singular: 'Course' },
omitNull: false,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [SqliteDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: [Object] },
hooks: {} },
_options:
{ isNewRecord: true,
_schema: null,
_schemaDelimiter: '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined },
isNewRecord: false }
Executing (default): SELECT `Investing for beginners` FROM `Courses` AS `Course`;
{ SequelizeDatabaseError: SQLITE_ERROR: no such column: Investing for beginners

它告诉我我刚刚创建的类(class)不存在,我不知道为什么。

最佳答案

您可以在 dataValues 下看到刚刚创建了一个带有 id: 11 的新行。

您的问题是 findAllSequelize.Model 返回一个模型实例数组,而您尝试访问单个实例。
此外,您只要求提供 title 属性,然后您尝试访问 course.id。即,即使您正确访问了 findAll 的返回值,也不会在结果中找到 id 属性。

尝试根据以下示例修复您的代码:

app.post('/api/courses', async (req, res) => {
try {
const newCourse = req.body;
let course = await Course.create({
title: newCourse.title,
description: newCourse.description,
estimatedTime: newCourse.estimatedTime,
materialsNeeded: newCourse.materialsNeeded,
userId: 1
});

// This is redundant and wrong.
// const course = await Course.findAll({
// attributes: [newCourse.title]
// })

const courseParam = course.dataValues.id;
res.location(`/api/courses/${courseParam}`);
res.status(201);
}catch(error) {
console.warn(error);
}

});

关于sequelize.js - Sequelize : Column not found immediately after creation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698872/

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