【问题标题】:how to solve the problem Promise { <pending> } [duplicate]如何解决问题 Promise { <pending> } [重复]
【发布时间】:2021-10-10 11:49:53
【问题描述】:

我是下一个 JS 的新手,当我尝试在特定路线中显示我的数据时,它没有显示,谁能告诉我为什么它没有显示,如果您对我的问题有任何疑问,请随意提问.

import { getDataFromSheets } from "../../libs/sheets";

export default function handler(req, res) {

    const sheet =  getDataFromSheets();

    console.log(sheet)

    res.status(200).json(sheet);

}

【问题讨论】:

  • 您的getDataFromSheets 函数可能是异步的。这个问题中关于 async/await 和 promises 的部分是否回答了你的问题? How to return the response from an asynchronous call
  • 你好@Monu Patil,你能不能下次尝试避免发布包含代码的图像,而不是直接将输出复制为code文本。它可以提高问题的质量。谢谢
  • 你好@axel,你标记错人了:)
  • 啊,我的错,我现在修好了:)

标签: javascript reactjs google-sheets next.js


【解决方案1】:

使用可以通过多种方式解决这个问题,

使用异步/等待

import { getDataFromSheets } from "../../libs/sheets";

export default async function handler(req, res) {
    const sheet =  await getDataFromSheets();
    console.log(sheet)
    res.status(200).json(sheet);
}

使用 .then()、.catch()

import { getDataFromSheets } from "../../libs/sheets";

export default function handler(req, res) {
    getDataFromSheets()
      .then(sheet=> res.status(200).json(sheet))
      .catch(err=> console.log(err))
}

【讨论】:

    【解决方案2】:
    import { getDataFromSheets } from "../../libs/sheets";
    
    export default async function handler(req, res) {
      const sheet = await getDataFromSheets();
      console.log(sheet);
      res.status(200).json(sheet);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多