通过 Yahoo FInance API 将索引上传到 Algolia。即使我可以 console.log 也无法访问数组

发布于 2025-01-10 10:15:35 字数 1603 浏览 0 评论 0原文

问题是我可以从 useEffect 挂钩 console.log 数组,但是当我去访问它时,它返回一个空数组。

我正在尝试将数据上传到我的 Algolia 索引。

这是当前的 useEffect:

  useEffect(() => {
    const fetchListings = () => {
      api
        .listNasdaq()
        .then((response) => {
          setListings(response.data);
          console.log(response); //I can see the array of data here.
        })
        .catch((error) => {
          console.log(error);
        });
    };
    fetchListings();
  }, []);

api.js:

listNasdaq: () =>
    exchangeApi({
      method: "GET",
      url: "/companies/list-by-exchange",

      transformResponse: [
        function (data) {
          // Do whatever you want to transform the data
          console.log("Transforming Nasdaq Data...");

          const json = JSON.parse(data);

          const stocks = Object.keys(json["results"]);

          const stockNames = stocks.map(
            (stock) =>
              (stock = {
                stock,
                stockName: String(json["results"][stock]["companyName"]),
                symbol: String(json["results"][stock]["symbol"]),
                industry: String(json["results"][stock]["industryOrCategory"]),
              })
          );

          data = {
            stockNames,
          };

          return data;
        },
      ],
    }),

Algolia:

  nasdaqIndex
    .saveObjects(listings, {
      autoGenerateObjectIDIfNotExist: true,
    })
    .then(({ objectIDs }) => {
      console.log("algolia stuff", objectIDs);
    });

The problem is that I can console.log the array from the useEffect hook, but when I go to access it, it returns an empty array.

I am trying to upload data to my Algolia index.

Here is the current useEffect:

  useEffect(() => {
    const fetchListings = () => {
      api
        .listNasdaq()
        .then((response) => {
          setListings(response.data);
          console.log(response); //I can see the array of data here.
        })
        .catch((error) => {
          console.log(error);
        });
    };
    fetchListings();
  }, []);

api.js:

listNasdaq: () =>
    exchangeApi({
      method: "GET",
      url: "/companies/list-by-exchange",

      transformResponse: [
        function (data) {
          // Do whatever you want to transform the data
          console.log("Transforming Nasdaq Data...");

          const json = JSON.parse(data);

          const stocks = Object.keys(json["results"]);

          const stockNames = stocks.map(
            (stock) =>
              (stock = {
                stock,
                stockName: String(json["results"][stock]["companyName"]),
                symbol: String(json["results"][stock]["symbol"]),
                industry: String(json["results"][stock]["industryOrCategory"]),
              })
          );

          data = {
            stockNames,
          };

          return data;
        },
      ],
    }),

Algolia:

  nasdaqIndex
    .saveObjects(listings, {
      autoGenerateObjectIDIfNotExist: true,
    })
    .then(({ objectIDs }) => {
      console.log("algolia stuff", objectIDs);
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-01-17 10:15:35

我对你的地图功能有点困惑。您是否将完整的 stock 对象注入回自身((stock = { stock, ...))?当您记录它时,最终的stock 对象是什么样子?我我担心当您保存记录时,Algolia 可能会感到困惑。

          const stockNames = stocks.map(
            (stock) =>
              (stock = {
                stock,
                stockName: String(json["results"][stock]["companyName"]),
                symbol: String(json["results"][stock]["symbol"]),
                industry: String(json["results"][stock]["industryOrCategory"]),
              })
          );

另外,我假设对 nasdaqIndex.saveObjects() 的调用发生在 setListings() 中?就像这里可能缺少一些代码一样。

I'm a little confused by your map function. Are you injecting the full stock object back into itself ((stock = { stock, ...)? What does the final stock object look like when you log it? I'm worried that might be confusing Algolia when you go to save the record.

          const stockNames = stocks.map(
            (stock) =>
              (stock = {
                stock,
                stockName: String(json["results"][stock]["companyName"]),
                symbol: String(json["results"][stock]["symbol"]),
                industry: String(json["results"][stock]["industryOrCategory"]),
              })
          );

Also, I assume the call to nasdaqIndex.saveObjects() occurs in setListings()? It looks like some of this code may be missing here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文