Sequelize.js Sequelize:创建后未立即找到列

Sequelize.js Sequelize:创建后未立即找到列,sequelize.js,Sequelize.js,在同一个post路径中,我尝试创建一个新课程,然后通过findAll()方法选择该课程。我这样做是因为我想为jsut创建的课程将location头设置为正确的URI。为此,我需要访问自动生成的id。问题是,当我调用findAll()方法时,它说我刚刚创建的课程没有找到 app.post('/api/courses', async (req, res) => { try { const newCourse = req.body; await Co

在同一个post路径中,我尝试创建一个新课程,然后通过findAll()方法选择该课程。我这样做是因为我想为jsut创建的课程将location头设置为正确的URI。为此,我需要访问自动生成的id。问题是,当我调用findAll()方法时,它说我刚刚创建的课程没有找到

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发表评论后更新 我将新创建的课程记录到控制台,它返回以下内容:

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

它告诉我,我刚刚创建的课程不存在,我不知道为什么

您可以在
dataValues
下看到刚刚创建了一个id为
11
的新行

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

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

app.post('/api/courses',异步(req,res)=>{
试一试{
const newCourse=req.body;
让课程=等待课程。创建({
标题:newCourse.title,
description:newCourse.description,
估计时间:newCourse.estimatedTime,
所需材料:新课程。所需材料,
用户ID:1
});
//这是多余和错误的。
//const course=wait course.findAll({
//属性:[newCourse.title]
// })
const courseParam=course.dataValues.id;
res.location(`/api/courses/${courseParam}`);
物质状态(201);
}捕获(错误){
控制台。警告(错误);
}
});

Model.create
返回创建的实例。尝试记录
等待课程的返回值。创建
以查看发生了什么。我尝试了你所说的,并在上面发布了结果。如果里面有一些有用的信息,我不确定是什么。有什么想法吗?谢谢