遇到节点和异步问题

时间:2022-11-25 03:44:44

I'm trying to excute async-waterfall to get api result and save it to json then save it to database, thats snippet of my code Please Help!

我正在尝试执行async-waterfall来获取api结果并将其保存到json然后将其保存到数据库,这就是我的代码片段请帮忙!

    async.waterfall([
    function getBook(cbAsync) {
      books.search(query, (err, result) => {
        if (err)
          cbAsync(err)
        res.json(result)
      })
    },
    function saveToJson(saveToJsonCb, cbAsync) {
      jsonfile.writeFile(file, result, (err) => {
        if (err)
          cbAsync(err)
      })
    },
    function SaveToDb(saveCb, saveToJsonCb, cbAsync) {
      const book = {
        title: res.body.title,
        authors: [res.body.authors],
        description: res.body.description
      }
      //save the bookInfo to db
      book.save( (err) => {
        if (err)
          cbAsync(err)
        console.log('Book added!')
      })
    }
  ], function asyncComplete(err) {
    if (err) {
      console.warn('Error')
    } else {
      console.info('Task complete with success')
    }
  })

1 个解决方案

#1


0  

The first task only calls the callback if it encounters an error, which in a perfect condition, will never be called.

第一个任务只有在遇到错误时才会调用回调,这个错误在完美的情况下永远不会被调用。

Note:
1. Every 'task' must call a callback.
2. Make sure that the callback is called before each function finishes.

注意:1。每个“任务”都必须调用回调。 2.确保在每个函数完成之前调用回调。

Follow the pattern indicated in the Async Documentation http://caolan.github.io/async/docs.html#waterfall

按照异步文档http://caolan.github.io/async/docs.html#waterfall中指示的模式进行操作

#1


0  

The first task only calls the callback if it encounters an error, which in a perfect condition, will never be called.

第一个任务只有在遇到错误时才会调用回调,这个错误在完美的情况下永远不会被调用。

Note:
1. Every 'task' must call a callback.
2. Make sure that the callback is called before each function finishes.

注意:1。每个“任务”都必须调用回调。 2.确保在每个函数完成之前调用回调。

Follow the pattern indicated in the Async Documentation http://caolan.github.io/async/docs.html#waterfall

按照异步文档http://caolan.github.io/async/docs.html#waterfall中指示的模式进行操作