Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js ExpressJS-重构嵌套回调并处理app.get函数中的错误?_Node.js_Express - Fatal编程技术网

Node.js ExpressJS-重构嵌套回调并处理app.get函数中的错误?

Node.js ExpressJS-重构嵌套回调并处理app.get函数中的错误?,node.js,express,Node.js,Express,我是Node.js的新手,并且已经因为嵌套回调而感到沮丧,因为嵌套回调使得读取代码和排除拼写错误非常困难 正如你们在下面看到的,我有两个相关的模型(Blog和Comment)和app.get方法,我为一篇博客文章创建了评论 Model Structure: Blog ..title (string) ..blog (string) ..comments (Referenced Comment Model) ....comment (string) Comment ..comment(stri

我是Node.js的新手,并且已经因为嵌套回调而感到沮丧,因为嵌套回调使得读取代码和排除拼写错误非常困难

正如你们在下面看到的,我有两个相关的模型(Blog和Comment)和app.get方法,我为一篇博客文章创建了评论

Model Structure:
Blog
..title (string)
..blog (string)
..comments (Referenced Comment Model) 
....comment (string)

Comment
..comment(string)
当前app.get方法有3个嵌套的回调函数,可能的错误只记录在console.logged中(如果我开始为错误编写更多代码,那么会更好地使用用户体验,因为函数会变得一团糟)


在这里,我想征求您的建议,以简化下面的函数,以及如何更好地处理错误。

正如其他人所评论的,承诺是一种方式,而异步/等待通常是编写承诺的最优雅的方法。例如,您的代码可以压缩为以下内容。您应该仔细阅读承诺,因为它们是节点开发的一个重要概念

app.post('/blog/:id/comment', async function(req,res){
  try{
   const newComment = await Comment.create(req.body.comment);
   const foundBlog = await Blog.findById(req.params.id);

   foundBlog.comments.push(newComment);
   await foundBlog.save();
   res.redirect('/blog/'+req.params.id);
  }
  catch(err){
   console.log(err);
  }      

});

正如其他人所评论的,承诺是一种方式,异步/等待通常是编写承诺的最优雅的方法。例如,您的代码可以压缩为以下内容。您应该仔细阅读承诺,因为它们是节点开发的一个重要概念

app.post('/blog/:id/comment', async function(req,res){
  try{
   const newComment = await Comment.create(req.body.comment);
   const foundBlog = await Blog.findById(req.params.id);

   foundBlog.comments.push(newComment);
   await foundBlog.save();
   res.redirect('/blog/'+req.params.id);
  }
  catch(err){
   console.log(err);
  }      

});

看起来您正在使用Mongoose,它支持承诺,因此您可以执行以下操作:

app.post('/blog/:id/comment',(req,res) {
  Comment.create(req.body.comment)
    .then(newComment => {
      return Blog.findById(req.params.id))
        .then(foundBlog => {
          foundBlog.comments.push(newComment)
          return foundBlog.save()
        })
    })
    .then(() => res.redirect('/blog/' + req.params.id))
    .catch(err => console.log(err))
})
您还可以使用异步等待:

app.post('/blog/:id/comment', async (req, res) {
  try {
    const newComment = await Comment.create(req.body.comment)
    const foundBlog = await Blog.findById(req.params.id)
    foundBlog.comments.push(newComment)
    await foundBlog.save()
    res.redirect('/blog/' + req.params.id)
  }
  catch(err) {
    console.log(err)
  }
})

看起来您正在使用Mongoose,它支持承诺,因此您可以执行以下操作:

app.post('/blog/:id/comment',(req,res) {
  Comment.create(req.body.comment)
    .then(newComment => {
      return Blog.findById(req.params.id))
        .then(foundBlog => {
          foundBlog.comments.push(newComment)
          return foundBlog.save()
        })
    })
    .then(() => res.redirect('/blog/' + req.params.id))
    .catch(err => console.log(err))
})
您还可以使用异步等待:

app.post('/blog/:id/comment', async (req, res) {
  try {
    const newComment = await Comment.create(req.body.comment)
    const foundBlog = await Blog.findById(req.params.id)
    foundBlog.comments.push(newComment)
    await foundBlog.save()
    res.redirect('/blog/' + req.params.id)
  }
  catch(err) {
    console.log(err)
  }
})

学会爱。他们的主要目的之一是帮助你重构。为此使用
承诺
。学会爱。他们的一个主要目的是帮助您重构,而不是使用
promissions