繁体   English   中英

NodeJS返回的数据有时会更改

[英]NodeJS returned data sometimes changes

我是NodeJS的新手,但有一个我不明白的问题。

在此函数中,我一个接一个地调用几个API,以检索有关电影的一些数据。 结果并不总是相同的。 大多数情况下,结果是正确的,但有时结果不完整。

我尝试使用then尝试链接API调用,但似乎不起作用。

知道为什么结果并不总是相同的吗? 任何帮助,将不胜感激。

// test fetchData(456165)

function fetchData(filmid) {

let average = array => array.reduce((a, b) => a + b) / array.length
var notes = []

mdb.movieInfo({
    id: filmid,
    language: 'fr'
  },
  (err, resOmdb) => {
    notes.push(parseFloat(resOmdb.vote_average))
    imdb
      .getById(resOmdb.imdb_id, {
        apiKey: 'e9d59b68',
        timeout: 3000
      })
      .then(
        allocine.api(
          'search', {
            q: `${resOmdb.title}`,
            filter: 'movie'
          },
          function(error, resAllo) {
            if (error) {
              return
            }

            allocine.api(
              'movie', {
                code: `${resAllo.feed.movie[0].code}`
              },
              function(error, result) {
                if (error) {
                  return
                }
                notes.push(parseFloat(result.movie.statistics.userRating) * 2)
              }
            )

            // doesn't seem to execute all the time
            allocine.api(
              'showtimelist', {
                zip: 44260,
                movie: resAllo.feed.movie[0].code
              },
              function(error, resultCin) {
                if (error) {
                  return
                }

                // sometimes doesn't appear in the result
                resOmdb.cinemas = resultCin
              }
            )

          }
        )
      )
      .then(
        function(result) {
          notes.push(parseFloat(result.rating))
          resOmdb.vote_average = average(notes).toFixed(2)

          // check the result
          console.log(util.inspect(resOmdb, false, null))
        },
        function(error) {
          return
        }
      )
  }
)
}

首先,您应该决定是否要使用Promises。 如果这样做,则使所有功能合理化。 接下来需要做的就是“兑现”您的诺言(如果在函数内部使用它们)。

在您的情况下,可能不会返回您的第一个imbd api调用。

接下来,您应该检查您的节点版本是否支持异步等待。

然后,您可以轻松进行api调用,而不会分心。

'use strict';

const Promise = require('bluebird');
const mdb = Promise.promisfyAll(require('mdb'));
const allocine = Promise.pomisifyAll(require('allocine-api'));

// test fetchData(456165)

async function fetchDate(filmId) {
  const notes = [];
  const resOmdb = await mdb.movieInfoAsync({ id: filmId });
  notes.push(parseFloat(resOmdb.vote_average));

  const imdbResult = await imdb.getByIdAsync(resOmdb.imdb_id, { apiKey: 'e9d59b68', timeout: 3000 });

  const resAllo = await allocine.apiAsync('search', { q: `${resOmdb.title}`, filter: 'movie' });
  // and so on ...
}

更新:为了加快您的功能,您可以同时执行请求。 为此,请使用Promise.join

  const [imdbResult, allocineResult] = await Promise.join(
    imdb.getByIdAsync(resOmdb.imdb_id, { apiKey: 'e9d59b68', timeout: 3000 }),
    allocine.apiAsync('search', { q: `${resOmdb.title}`, filter: 'movie' });
  );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM