从路由器获取变量时,它将返回requestProvider.js.map in node.js中
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您面临的问题是,您的零检查文章实际上并没有终止功能过程,因此稍后您在调用Article时。您可以通过在响应的前面添加返回来解决此问题。
或总体使用可选的链式
,您应该尝试端点的重构,我建议:
注意:这是未经测试的
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.
or using optional chaining
overall you should try a refactor of the endpoint, I'd suggest:
note: This is untested
当我使用混音并得知您的网页上 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.