【问题标题】:how to get data from Async function, It will return "[object Promise]" instead of data [duplicate]如何从异步函数中获取数据,它将返回“[object Promise]”而不是数据 [重复]
【发布时间】:2021-07-02 19:52:20
【问题描述】:

当我调用函数时,它将 [object Promise] 而不是数据,我认为 Promise 将处于挂起状态,然后返回。谁能帮助我以正确的方式做到这一点?

这是我的 util 函数。

export const GetDataFromSnapshot = async (snapshot) => {
  const data = [];
  try {
    return await snapshot.onSnapshot((docs) => {
      const currentState = [];
      docs.forEach((doc) => {
        currentState.push(doc.data());
      });
      data.push(currentState);

      console.log(currentState);
      // 0:
      //   id: "71u638UFCpGs6hLkWP"
      //   timestamp: t {seconds: 2177433000, nanoseconds: 0}
      //   title: "Remind Me in future"

    });
  } catch (error) {
    console.log(error);
  }
};

这个由 saga-watcher 生成器函数调用的函数看起来像这样。

// TODO: action calling the API
function* fetchReminder() {
  try {
    const reminders = yield call(GetAllReminders);

    const resultDoc = GetDataFromSnapshot(reminders);

    console.log(`resultDoc =>> ${resultDoc}`);
    // resultDoc =>> [object Promise]

   resultDoc
      .then((data) => {
        console.log(`resultDoc =>> ${resultDoc}`);
        // resultDoc =>> [object Promise]

        console.log(`data =>> ${data}`);
        // data =>> function () {
        //           i.Zl(), r.cs.ws(function () {
        //             return Pr(r.q_, o);
        //          });
        //         }

        data.json();
        // Unhandled Rejection (TypeError): data.json is not a function

      })
      .then((response) => console.log(`response =>> ${response}`));

    yield put(setReminder(reminders));

如何获取数据将如下所示。来自 firestore 数据库的对象数组

0:
  id: "71u638UFCpGs6hLkWP"
  timestamp: t {seconds: 2177433000, nanoseconds: 0}
  title: "Remind Me in future"

【问题讨论】:

  • 看看我的回答,应该可以帮到你:)
  • 你的代码写得好像snapshot.onSnapshot() 返回一个promise,它解析为具有.json() 方法的东西。这似乎不太可能,看起来更像是您刚刚复制粘贴了一些示例代码。在我看来 data 数组是你想从 GetDataFromSnapshot() 返回的东西,如果 .onSnapshot() 没有返回你需要自己promisify的承诺。
  • @Lennholm,你做得对,但是我自己如何承诺,你能帮我快速理解你所说的承诺和所有东西的代码或任何链接。
  • @Lennholm,你是对的 .onSnapshot() 在此处正确返回 [object Promise]。我又检查了一遍。但是我在这里没有得到.json() 的值。

标签: javascript reactjs firebase firebase-realtime-database


【解决方案1】:

只需在此执行下一个.then(),您将获得数据,但您必须使其成为data.text()data.json(),这取决于它是什么数据。比如:

      resultDoc.then((data) => {
         data.json();
      ).then((response)=> console.log(response);

【讨论】:

  • data =>> , resultDoc =>> [object Promise],数据为空,resultDoc 为对象承诺。当我们data.json() 时,它会出现未处理的拒绝错误,例如; data.json() is not a function obevisouly
  • 如果它是 [object Promise] 你可以做 .then on it 或者做 await,仅此而已
  • export const GetDataFromSnapshot = async (snapshot) => { const result = new Promise((resolve, reject) => { const data = []; snapshot.onSnapshot( (docs) => { docs.forEach((doc) => { data.push(doc.data()); }); resolve(data); }, (error) => { console.log(error); reject(); } ); }); const finalResult = await result; return finalResult; };
  • const resultDoc = GetDataFromSnapshot(reminders); resultDoc.then((res) => {console.log(res); put({ type: 'FETCH_REMINDER', reminders: fetchReminders(res) }); }) .catch((err) => { console.log(err); }); 然后它对我有用
猜你喜欢
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2019-12-29
  • 2019-10-18
  • 2019-10-23
  • 2015-08-15
  • 2023-03-08
  • 2018-06-24
相关资源
最近更新 更多