【问题标题】:Argument passed in must be a single String of 12 bytes or a string of 24 hex characters?传入的参数必须是 12 个字节的单个字符串还是 24 个十六进制字符的字符串?
【发布时间】:2021-03-08 13:17:12
【问题描述】:

''' 这是我删除任务的端点,但由于某些问题,当我从邮递员那里尝试时它不起作用。当我通过硬代码(静态)输入 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()
}

})

【问题讨论】:

  • 你把ID打印出来看看是什么吗?

标签: javascript node.js express rest objectid


【解决方案1】:

使用 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()
}

【讨论】:

  • 感谢 Namit 的独奏,但我能问一下为什么我会得到一个额外的 '\n' 吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2020-09-01
  • 1970-01-01
相关资源
最近更新 更多