Node.js https.get和响应对象的打字稿问题

时间:2020-05-05 01:59:37

标签: node.js typescript

我在打字稿中遇到与此代码有关的问题,想知道可能是什么问题。在注释中查找错误消息时,我找不到任何实质性的帮助。还有其他错误,但是它们是我已经包含的错误的重复。

    https.get(personUrlByName + name, "utf8", async (res: Response) => { // type "utf8" has no properties in common with type 'RequestOptions'
      const { statusCode } = res; // Property 'statusCode' does not exist on type 'Response'
      let error;
      if (statusCode !== 200) {
        error = new Error(`request failed.\n Status Code: ${statusCode}`);
      }
      if (error) {
        reject(error);
        res.resume();
        return;
      }
      let rawData = "";
      for await (const chunk of res) { // type 'Response' must have a '[Symbol.asyncIterator]()' method that returns an async iterator
        rawData += chunk;
      }
      try {
        const parsedData = JSON.parse(rawData);
        const results = parsedData?.results;
        results.length === 1 && resolve(results[0]);
      } catch (e) {
        reject(e);
      }
    });

0 个答案:

没有答案