我正在尝试从 Mongodb 获取数据,我希望该数据显示在我的网页上。问题是我不知道如何将整个获取的对象发送到特定端口(响应),以便我'将能够从 Angular 获取它以及我如何能够从 Angular 访问数据
从 Mongodb 获取数据
app.get('/api/getdata',async (req,res) =>
{
const {value} = req.body
const resp=await person.find({value})
if(!resp){
console.log('not found')
}
else{
//this needs to be done
}
})
最佳答案
那么你的代码将如下所示:
app.get('/api/getdata', async (req,res) => {
const {value} = req.body
const resp = await person.find({value})
if (!resp) {
res.status(404).send('not found')
}else{
// give your data to express response to the http request
res.json(resp); // or res.send(resp);
}
});
关于javascript - 从 Angular 中的 Node.js 获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008631/