node.js - 数据已添加到数据库,但邮递员返回空白。我错过了什么?
问题描述
我正在尝试通过使用 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})
})
});
解决方案
为什么在插入数据后不使用 .returning('*') 而不是 .then 呢?
另外,我相信插入数据库是一个异步命令,但我没有看到在任何地方使用异步。
推荐阅读
- python - CoreML 中 Python Tensorflow 模型的图像输入
- visual-studio-code - 如何在 VS CODE 扩展中提供具有“低优先级”的完成项?
- java - Spring Boot TCP 客户端
- angular - 在结构指令中获取元素绑定
- php - 如何将 Active WooCommerce Membership Plan Slug 添加到 Body Class?
- sass - 如何使用 Hugo 设置 SCSS
- c++ - 如何用 strcpy_s 替换 strcpy?
- ruby-on-rails - 如何将数据从 ajax() 传递到控制器 Rails
- dart - 在课堂内从 Firebase 注销
- ruby - Extract hex strings from binary file in Ruby