我无法从服务器获取数据

问题描述 投票:0回答:1

我的代码在下面,我正在使用node.js并检查邮递员,然后显示错误,请帮助我

app.get('/tasks/:id', (req,res)=>{
    const _id =  req.params.id;
    console.log(_id);
    const task =   Task.findById(_id);
    console.log(task);
    if (task) {
        res.status(200).send(task);
    }
    else {
        res.status(400).send("Not Found")
    }
node.js mongodb express postman
1个回答
0
投票

嘿,您必须如上所述使用异步函数,这将无法从服务器使用以下代码获得响应:

app.get('/tasks/:id', async(req,res)=>{
    const _id = req.params.id;
    console.log(_id);
    const task = await Task.findById(_id);
    console.log(task);
    if (task) {
        res.status(200).send(task);
    }
    else {
        res.status(400).send("Not Found")
    }

希望它解决了您的问题

© www.soinside.com 2019 - 2024. All rights reserved.