简体   繁体   English

Promise.all引发TypeError:undefined不是函数

[英]Promise.all raises TypeError: undefined is not a function

I'm returning a list of promises from a function, and I need to get the values of that array. 我从函数返回一个诺言列表,我需要获取该数组的值。 Therefore, I'm using the Promise.all function. 因此,我正在使用Promise.all函数。 But this doesn't work very well for me. 但这对我来说不是很好。 I'm getting this error: 我收到此错误:

Uncaught (in promise) TypeError: undefined is not a function
    at Function.all (<anonymous>)
    at new MySearchesTable (my-searches-table.js:162)
    at constructClassInstance (react-dom.development.js:6801)

Here's where the error originates from. 这是错误的来源。

class MySearchesTable extends React.Component {
  constructor(props, context) {
      super(props, context);
      let data = Promise.all(modelInstance.getSearchHistory()); // Error traceback starts here!
      this.state = {
        selected: [],
        data: data,
        page: 0,
        rowsPerPage: 5,
        open: false,
      };

And here's the function that returns a list of promises: 这是返回承诺列表的函数:

  /*
  * Gets searches for logged in user
  */
  this.getSearchHistory = function(){

    let currUserSearches;
    return new Promise((resolve, reject)=>{
      firebase.auth().onAuthStateChanged(function(user){
      if(user){
        let uid = user.uid;

        let userRef = database.ref("users/"+uid);

        return userRef.once("value")
        .then( (value) => {
          let currUserSearchesIDs = value.val();
          console.log("currUserSearches:");
          let currUserSearches = currUserSearchesIDs.map( searchID => {
            return database.ref("searches/"+searchID).once("value")
                .then( (value) => {
                  let obj = value.val();
                  obj["id"] = searchID;
                  return obj;
                });
          });
          console.log(currUserSearches);
          resolve(currUserSearches);
        });
      }else{
        reject("Must log in"); // user must log in
      }
      }
    )
    });

  }

What is the reason for this error? 此错误的原因是什么? How can I successfully get a list of the values I have in my database? 如何成功获取数据库中的值列表?

The reason is that Promise.all expects an iterable . 原因是Promise.all期望iterable You are only returning 1 promise . 您只返回1个诺言

Look: 看:

在此处输入图片说明

In the above example I'm using Promise.all and passing a single Promise to it. 在上面的示例中,我使用Promise.all并将单个Promise传递给它。 The result of Promise.resolve(null) is 1 promise that resolves to null. Promise.resolve(null)的结果是1 promise解析为null。

The error doesn't occur when you pass an array of Promise like: 当您传递一个Promise数组时,不会发生该错误:

Promise.all([Promise.resolve(null)])

That being said, I must point out that you should reconsider your design, because you are not supposed to wait on promises on the constructor. 话虽如此,我必须指出,您应该重新考虑您的设计,因为您不应该等待构造函数的承诺。 Use componentDidMount for that. 为此使用componentDidMount The first render of your component should have data as null, and eventually display a loading indicator. 组件的第一个渲染器的data应为null,并最终显示一个加载指示器。 On componentDidMount , trigger the data fetching part. componentDidMount ,触发数据获取部分。 Once the data is returned, re-render your component, now with the data already fetched. 返回数据后,重新渲染您的组件,现在已经获取了数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM