您可以附加多个“获取”请求的结果并将其作为“获取”响应发送出去吗?

在过去的几个小时里,我一直在玩这个游戏,我一生都无法弄清为什么它返回空列表。

'''

function getFile(file) {
    var request = require("request");
    request(
      {
      uri:
          file,
      method: "GET",
      gzip: true,
      qs: {
          format: "json",
      },
      },
      (err, resp, body) => {
        console.log(body);
        return body
      }
    )
}

async function download_tire_json_from_firebase() {

  let files = ['https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fforceum.json?alt=media&token=ed9db67c-d744-414f-8812-21cfbba63447',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fgood-year.json?alt=media&token=1131b553-e2e5-4ff6-87cb-f346e57e389e',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fgt-radial.json?alt=media&token=d2ba3f51-cef7-494e-8006-61d321d677cd',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fkpatos.json?alt=media&token=75743c44-1521-4ff4-8133-e56a474f7c3e',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fland-spider.json?alt=media&token=22a3aa7b-18a9-4bd1-b34b-5ae48cadbc55',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fleao-tires.json?alt=media&token=84dcc7f8-de32-46a9-9aae-854639772975',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fwanli.json?alt=media&token=4bb13f3a-5d9a-4c2d-b558-964ee996a534',
                'https://firebasestorage.googleapis.com/v0/b/moberra-tire-api.appspot.com/o/tire-data-eco%2Fzeta.json?alt=media&token=c04f973c-2577-4f06-805e-68e46480e9ae'
              ]
  const results = []

  for(const file of files) {
    results.push(getFile(file));
  }
  console.log("Final Result:", results)
  return await Promise.all(results)
}

app.get("/get-tire-data", async (req, res) => {
  let results = await download_tire_json_from_firebase()
  res.send(JSON.stringify(results))
})

'''

奇怪的是,在我的节点控制台中,console.log(“ Final Result:”,results)会打印出期望的列表。好像该列表未将其添加到“获取轮胎数据” get请求中。

另外,如果有人知道如何简化上面的代码,我也将非常感谢。

提前致谢,

〜阿卜杜勒。

评论
  • 钻石泪
    钻石泪 回复

    You need to return a Promise in getFile

    function getFile(file) {
        var request = require("request");
        return new Promise((resolve, reject) => {
          request(
            {
            uri:
                file,
            method: "GET",
            gzip: true,
            qs: {
                format: "json",
            },
            },
            (err, resp, body) => {
              if (err) {
                reject(err)
              } else {
                resolve(body)
              }
            }
          )
        })
    }