从路由器获取变量时,它将返回requestProvider.js.map in node.js中

发布于 2025-01-23 10:59:22 字数 1268 浏览 0 评论 0原文

我有一个node.js应用。当网页第一次渲染时,所有人都按预期工作,但是当检查应用程序崩溃时,req.params.slug显示为requestProvider.js.map。

router.get('/:slug', async (req, res) => {
  const article = await Article.findOne({ slug: req.params.slug })
  if (article == null){
    res.render('/')
  } 
  res.render('articles/show', { article: article })
})

编辑 使用控制台。log消息

router.get('/:slug', async (req, res) => {
  console.log("slug")
  console.log(req.params)
  const article = await Article.findOne({ slug: req.params.slug })
  console.log("article")
  console.log(article)
  if (article == null){
    res.render('/')
  } 
  console.log("article")
  console.log(article)
  console.log("title")
  console.log(article.title)
  res.render('articles/show', { article: article })
})

控制台消息是

slug {slug:'requestProvider.js.map'} 文章 无效的 文章 无效的 标题 c:\ users \ samue \ oneDrive \ desktop \ shortcuts and utused \ unused \ unused 2 \ blog \ public \ public \ routes \ articles.js:32 console.log(Article.title) ^

TypeError:无法读取空的属性(读取'title') 在C:\ users \ samue \ oneDrive \ desktop \ shortcuts和未使用\未使用的2 \ blog \ public \ doutes \ artices.js:32:23 在processTickSandReptions(节点:内部/process/task_queues:96:5) [nodemon]应用程序崩溃了 - 启动之前等待文件更改...

I have a node.js app. When the webpage is rendered the first time all works as expected, but when inspecting the app crashes, and the req.params.slug shows as requestProvider.js.map.

router.get('/:slug', async (req, res) => {
  const article = await Article.findOne({ slug: req.params.slug })
  if (article == null){
    res.render('/')
  } 
  res.render('articles/show', { article: article })
})

Edit
With Console.Log Messages

router.get('/:slug', async (req, res) => {
  console.log("slug")
  console.log(req.params)
  const article = await Article.findOne({ slug: req.params.slug })
  console.log("article")
  console.log(article)
  if (article == null){
    res.render('/')
  } 
  console.log("article")
  console.log(article)
  console.log("title")
  console.log(article.title)
  res.render('articles/show', { article: article })
})

The console messages are

slug
{ slug: 'requestProvider.js.map' }
article
null
article
null
title
C:\Users\samue\OneDrive\Desktop\shortcuts and unused\Unused 2\Blog\public\routes\articles.js:32
console.log(article.title)
^

TypeError: Cannot read properties of null (reading 'title')
at C:\Users\samue\OneDrive\Desktop\shortcuts and unused\Unused 2\Blog\public\routes\articles.js:32:23
at processTicksAndRejections (node:internal/process/task_queues:96:5)
[nodemon] app crashed - waiting for file changes before starting...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2025-01-30 10:59:22

您面临的问题是,您的零检查文章实际上并没有终止功能过程,因此稍后您在调用Article时。您可以通过在响应的前面添加返回来解决此问题。

if (article == null) return res.render('/') 

或总体使用可选的链式

console.log(article?.title)

,您应该尝试端点的重构,我建议:

router.get('/:slug', async (req, res) => {

  const { slug } = req.params        // destructure slug from params
  if (!slug) return res.render('/')  // check if provided

  try {
    const article = await Article.findOne({ slug })
    return res.render('articles/show', { article })
  } catch(err) {
    res.status(400) // or some other error repsonse
    return res.render('your error page')
  }
})

注意:这是未经测试的

The problem you're facing is that your null check for article doesn't actually end the function process, so later on when you call article.title it throws undefined. You can fix this by adding return infront of the response.

if (article == null) return res.render('/') 

or using optional chaining

console.log(article?.title)

overall you should try a refactor of the endpoint, I'd suggest:

router.get('/:slug', async (req, res) => {

  const { slug } = req.params        // destructure slug from params
  if (!slug) return res.render('/')  // check if provided

  try {
    const article = await Article.findOne({ slug })
    return res.render('articles/show', { article })
  } catch(err) {
    res.status(400) // or some other error repsonse
    return res.render('your error page')
  }
})

note: This is untested

我还不会笑 2025-01-30 10:59:22

当我使用混音并得知您的网页上 Chrome Developer Tools Open 时,这也会发生这种情况。 /descorions/35199?sort = top“ rel =“ nofollow noreferrer”> https://github.com/vercel/next.js/discussions/35199?sort = top 您将必须异常处理requestProvider.js.map被传递给您的URL并避免使用。

This also happened when I was using Remix and learned that it happens when you have Chrome Developer tools open for your web page: https://github.com/vercel/next.js/discussions/35199?sort=top (this is Next.js example) so you would have to exceptionally handle requestProvider.js.map being passed to your url and avoid it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文