传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串?

Argument passed in must be a single String of 12 bytes or a string of 24 hex characters?

''' 这是我删除任务的端点,但由于某些问题,当我从邮递员那里尝试时它不起作用。当我通过硬编码(静态)输入 ID 时,它的工作。 '''

router.delete('/tasks/:id', async (req, res) => { 试试{

    const task = await Task.findByIdAndDelete(req.params.id) //[![Why my objectid is not valid. It's working fine for other routes ][1]]
    if (!task) {
        res.status(404).send()
    }

    res.send(task)
} catch (e) {
    res.status(500).send()
}

})

对 javascript 中的字符串使用可用的 trim 方法,您将在 objectId 的末尾获得一个额外的 '\n' 字符。

const {id} = req.params; // destructuring in javascript
 const task = await Task.findByIdAndDelete(id.trim()) // trim the string 
    if (!task) {
        res.status(404).send()
    }
    res.send(task)
} catch (e) {
    res.status(500).send()
}