为什么 jquery-3.5.1.min.js 从 request.params.id 表达式返回

Why is jquery-3.5.1.min.js being returned from a request.params.id expression

我将 Express js 与类型脚本一起用于学校项目的简单应用程序。 这个路由器得到的是我们必须制作的联系人列表的编辑页面。它显示正在搜索栏中编辑的当前联系人的 ID。问题是当我想从它输出 2 个值的请求(请求)中获取 ID 时。其中一个是我想要的 ID,另一个是一些 jquery js 文件版本,我不知道如何解决这个问题。

router.get("/edit/:id", async(req, res) => {
let id = req.params.id;

let editContact = await Contact.findById(id, {}, {}, (err, itemForEdit) => {
    if (err) {
        console.log(err)
    } else {
        console.log("Profile That will be edited:", itemForEdit)
        res.render('../Views/Content/editContact.ejs', { title: 'Home Page', Login: LoginValue, username: currentUser, Contacts: ContactList });
    }
}); 
})

当 console.log 显示时,它会显示我正在尝试抓取的对象,然后还会显示此消息。

UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "jquery-3.5.1.min.js" (type string) at path "_id" for model "contactSchema"

当我执行 console.log(req.params) 时,它显示 2 个参数对象:

{ id: 'jquery-3.5.1.min.js' } (NOT THE ONE IM LOOKING FOR)

{ id: '60ca19ec4984810cec017b05' } (THIS IS THE ONE I WANT)

我没有这个叫任何其他我能想到的并且已经检查过很多次的商品。

请帮助!

您的网页似乎有一个脚本元素(<script> 标记)引用了一个 JQuery 包。您可以验证 id 以检查它是否是有效的用户 ID(如 /[a-z][a-z_0-9]+/i.test("id"));然后,如果有效,则解析为用户,否则解析为使用 Node.js 文件系统模块的文件。

在特定的 HTTP 端点提供脚本会更容易(不同于 /edit/xxx.js)。您可以使用 sirv 中间件,例如:

import Sirv from 'sirv';

router.use('/js', Sirv('./res/js', {
    dev: true,
}))

然后确保 <script> 标签引用 /js/jquery.js(注意开始斜线)。