我对Express的'res.send(string)'的调用在完成对MongoDB的'deleteMany()'的调用后无法重定向并显示所提供的文本。数据库已清除,并且未引发任何错误。
我试图用'res.json()'等来调整对'res'的调用,但无济于事。我还调整了“删除”请求中其他呼叫的顺序,但没有成功。我的倾向是我的问题与诺言有关。
.delete(function(req, res){
//if successful response will be 'complete delete successful'
console.log('deleting all documents');
MongoClient.connect(MONGODB_CONNECTION_STRING, { useNewUrlParser: true }, (connectErr, client) => {
if(connectErr) res.json({ "error": "Error connecting to database!", "error": connectErr });
const db = client.db('test-db');
try {
db.collection('testCollection2').deleteMany({}, (err) => {
if(err) throw err;
res.send('complete delete successful');
console.log('complete delete successful');
});
} catch(err) {
console.log('Complete delete failed!');
res.send('Complete delete failed!');
}
});
});
尽管未重定向或未收到错误消息,但我仍然收到console.log输出,确认已成功调用'deleteMany'。我不确定如何对此进行更深入的测试,因为我正在为项目使用Glitch。预先感谢您的帮助!