我正在尝试用javascript更新我的mongodb数据库,方法是访问数据库中的一些文档,更改特定的文档,然后通过axios执行补丁请求。
当我到达补丁请求时,我能够更新数据库,但是promise被挂起,因此,代码的then()部分没有运行。
这是代码的主要结构:在第一部分中,通过axios.get从数据库请求文档:
function updateDocument(someinputdata){
g = axios.all([axios.get('/getData1),axios.get('/getData2)])
.then(response => {
Data1 = response[0].data;
Data2 = response[1].data;
adjustData(Data1,Data2);
});
}
在第二部分中,更改了一个特定的文档,并调用了一个补丁请求:
function adjustData(Data1,Data2){
...getting specific document and change value from specific field...
var newRec = {
title: "dummyTitle",
rate: newRateValue
};
promise = axios({
url: '/patch/The Real Title',
method: 'PATCH',
data: newRec,
headers: { "Content-Type": "application/json" }
})
.then(() => {
console.log('I want this text to display but it doesn't')
});
}
If I console.log(promise):
Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
在服务器端,我有以下代码:
router.patch('/patch/:title', (req,res) => {
const updatedPost = Model.updateOne(
{ "title": req.params.title},
{ $set: { "rate" : req.body.rate}},
(err, result) => {
if(err) {
console.log(err);
throw err;
}
})
.then(
console.log('This text is displayed');
)
})
我想使用第一个then()部分来更新一些HTML为什么补丁请求被挂起(所以没有实现或被拒绝)?
发布于 2020-11-08 20:32:46
我已经知道我的问题出在哪里了。我需要添加
res.json({msg: "Your data has been saved"});
添加到服务器端的代码中。
https://stackoverflow.com/questions/64736978
复制相似问题