首页 > 解决方案 > 使用 Firebase 函数访问 API 数据

问题描述

我正在尝试从 API 中提取数据并将其存储在变量中。我正在使用 firebase 函数、IEXCloud 和 node.js,但我无法访问正文中的数据。该链接在我单独使用时有效,如果我从链接中删除 .body ,则该链接将发送到浏览器。

这就是我所拥有的:

const functions = require("firebase-functions");

const sA = "https://sandbox.iexapis.com/stable/";
const key = "(API-Key)";

exports.symbol = functions.https.onRequest((request, response) => {
  const symbol = (`${sA}stock/aapl/quote?token=${key}`).body;
  response.send(symbol);
});

标签: node.jsfirebaseapigoogle-cloud-functions

解决方案


您没有在这里调用任何 API const symbol = (${sA}stock/aapl/quote?token=${key} ).body;

你的意思是打电话给fetch()

exports.symbol = functions.https.onRequest((request, response) => {
  return fetch(`${sA}stock/aapl/quote?token=${key}`).then((res) => {
    symbol = res.body;
    response.send(symbol);
  });
});

推荐阅读