首页 > 解决方案 > 数据已添加到数据库,但邮递员返回空白。我错过了什么?

问题描述

我正在尝试通过使用 knex 执行 postgreSQL 操作来准备 api。

我的数据模型功能将保存数据,工作正常。在路由器方面,我成功地做到了,但邮递员返回给我一个空对象。

数据模型

function addArticle(newArticle) {
    return db("article")
        .insert(newArticle, "id")
        .then(([id]) => {
            return ("article").where({ id }).first();
        });
}

路由器

router.post('/addArticle',(req, res, next) => {
    const newArticle = req.body;

    Article.addArticle(newArticle)
        .then((article) =>{
            res.status(201).json(article)
        })
        .catch((err)=>{
            res.status(500).json({...err})
        })
});

邮差 在此处输入图像描述

标签: node.jspostgresqlexpresspostmanknex.js

解决方案


为什么在插入数据后不使用 .returning('*') 而不是 .then 呢?

另外,我相信插入数据库是一个异步命令,但我没有看到在任何地方使用异步。


推荐阅读