【发布时间】:2021-12-11 09:14:15
【问题描述】:
我正在向我的服务器发送一个简单的 fetch(),如下所示:
eo.put = function (state) {
console.log('PUT:state', state);
let _id = encodeURIComponent(state._id);
const options = {
headers: {'Content-Type': 'application/json'},
method: 'PUT',
body: JSON.stringify(state)
};
fetch("/articles/put/" + _id, options )
.then((response) => {
console.log('RESPONSE', response);
// you need to update the DOM here
})
.catch((error) => {
console.error("DEBUG: fetch/PUT error", error);
});
}
我可以验证状态是否填充了 console.log()。
但是;在服务器端 req.body 未填充并显示未定义。
// .. snip
app.use('/articles', routes.articles);
// .. snip
// UPDATE OPERATIONS
router.route('/put/:_id').put((req, res) => {
const _id = req.params._id;
const obj = req.body;
/*
**
** req.body is empty
**
*/
debug && console.log('DEBUG: route: /articles/put : req.body', req.body);
DBM.updateArticle(_id, obj).then(() => {
}).catch((err)=>{
res.status(500).send("DEBUG: database internal error", err);
});
res.end();
});
【问题讨论】:
-
网址似乎不匹配。
-
你还需要 JSON express 插件。
-
URL 匹配...我已经验证了该路由正在服务器上被命中...您只是在我上面的代码中看不到
/articles。 JSON express 插件是什么意思? -
我在上面的 app.use 命令中添加了... Express 可以在没有插件的情况下处理 JSON 我相信。
-
app.use(express.json()); ...我已经注释掉了这个命令,这破坏了身体...它被称为内置中间件...感谢您为我指出正确的方向...
标签: javascript reactjs express