javascript - 从 node.js 发送结果以做出反应
问题描述
因此listPage
,在我edit
的editPage
. 它现在确实这样做了。但我所做的是通过 发出请求axios.post
,以便将id
文档的 发送到后端,然后将结果发送到前端,在那里它只会根据它显示一个文档id
。这就是我所拥有的:
列表页:
const editById = (id) => {
console.log(id);
axios
.post(`/getDocToEdit`, { id: id })
.then(() => {
console.log(id, " worked");
window.location = "/admin/services/:site";
})
.catch((error) => {
// Handle the errors here
console.log(error);
});
};
然后它到达这个后端路由:
app.post('/getDocToEdit', (req, res) => {
var id = req.body.id;
ServicesModel.findOne({_id: id}, function(err,result) {
console.log(result);
res.status(200).send(result)
});
})
然后我只是想在我的屏幕上显示文档editPage
,但它不会加载我发送的结果res.status(200).send(result)
。我只有一张应该显示记录的表格。我应该再次从前端打电话还是什么?
解决方案
您应该将发布结果保存在前端:
const editById = (id) => {
console.log(id);
axios
.post(`/getDocToEdit`, { id: id })
.then((RESPONSE) => {
console.log(RESPONSE); // do it and if you have the response, everything
is fine and you can use it as the returned data
console.log(id, " worked");
window.location = "/admin/services/:site";
})
.catch((error) => {
// Handle the errors here
console.log(error);
});
推荐阅读
- visual-studio-code - 在 Visual Studio Code 中更改触发字符
- javascript - 使用 React Native 和 Image Crop Picker 同时上传到存储和数据库时 Firebase 错误代码 403
- javascript - 如何以相对方式在 React 表(父)中定位弹出框(子页面) - CSS/ReactJS/Javascript
- asp.net-core - 将参数从动作过滤器传递给控制器
- javascript - 发布后数据未提交到数据库
- amazon-web-services - 如何修复 AWS docker pull
- postgresql - docker/docker-compose tmpfs - pgsql_tmp 目录
- java - 不使用返回变量会导致内存泄漏
- android - 更新到 Fragment Testing 库 v1.1.0-alpha03 后崩溃
- excel - vbscript 检测和激活打开的 excel 电子表格