【问题标题】:mongoose findbyId() showing 'The "path" argument must be of type string. Received an instance of model'mongoose findbyId() 显示“路径”参数必须是字符串类型。收到模型实例'
【发布时间】:2021-10-13 18:10:46
【问题描述】:

这是我的代码(表达)

    app.get('/:id', async (req,res) => {
        const id = req.params.id;
        console.log(id)
        const bids = await Bid.findById(id)
        res.render(bids)
    
    })

这就是我在控制台中得到的

61671cef1c1db40b750502b0
(node:8068) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of model
    at validateString (internal/validators.js:124:11)
    at extname (path.js:828:5)
    at new View (D:\konectapp-1\node_modules\express\lib\view.js:56:14)
    at Function.render (D:\konectapp-1\node_modules\express\lib\application.js:570:12)
    at ServerResponse.render (D:\konectapp-1\node_modules\express\lib\response.js:1012:7)
    at D:\konectapp-1\app.js:62:9
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:8068) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我可以看到它在控制台顶部提供了我记录的对象 ID,但是当我将它传递给 findbyid 时,它给了我这个错误

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    ...但是当我将它传递给 findbyid 时,它给了我这个错误

    猫鼬findById() 似乎没有任何问题,尽管在使用async/awaittry/catch 块时最好正确处理错误。

    [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型...

    这来自res.render(bids)

    res.render() 只接受一个字符串,它是要渲染的视图文件的文件路径。但是在这里你传递一个对象作为参数。 See the documentation.

    【讨论】:

    • 除此之外,我怀疑他们真正想要做的是发送出价对象的 JSON 表示。您可以通过res.json(bids) 执行此操作,而不是使用模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2022-01-12
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多