简体   繁体   中英

I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

I'm trying to use a Promise.all in my node.js microservice. The intent of the Promise.all is to go through all the elements in an array (of queries), and via apolloFetch, calls another microservice, which then executes those queries in a database and then returns back success or error. I'm getting a 'Wrapped promise is not iterable' error - I checked a few posts on SO that have similar errors but in all those cases there are 2 arguments being passed, whereas I'm passing just one - EXCEPT that I'm using apolloFetch in order to connect to ANOTHER MICROSERVICE that takes each of those queries (in the array) and then performs some actions on a database.

Can someone figure out what I'm doing wrong here:

     const QryAllBooks = {
    type: new GraphQLList(BookType),
    args: {},
    resolve(){
          return new Promise((resolve, reject) => {
             let sql = singleLineString`
                  select distinct t.bookid,t.bookname,t.country
                  from books_tbl t
                  where t.ship_status = 'Not Shipped'
              `;
             pool.query(sql, (err, results) => {
               if(err){
                  reject(err);
               }
               resolve(results);

            const str = JSON.stringify(results);
            const json = JSON.parse(str);

            const promises = [];
            for (let p = 0; p < results.length; p++){
               const book_id = json[p].bookid;
               const query = `mutation updateShipping
                              {updateShipping
                               (id: ${book_id}, input:{
                                  status: "Shipped"
                               })
                               { bookid
                                 bookname }}`
                promises.push( query );
           }

          //I need an await function so that previous apolloFetch  
          //goes in sequence of bookid, one after the other

          Promise.all( promises.map(p=>apolloFetch({p})) ).then((result) => 
         {
                  resolve();
                  console.log("success!");
                  })
                 .catch(( e ) => {
                     FunctionLogError( 29, 'Error', e );
                 )};
                  });
            });
        }
      };

   module.exports = {
          QryAllBooks,
          BookType
   };

The code takes the return value from calling apolloFetch , and unconditionally feeds that to Promise.all .

I think the answer to:

Can someone figure out what I'm doing wrong here

is, you have not allowed for the case when apolloFetch returns something different from an iterable collection.

Instead, call apolloFetch , figure out whether or not the return value is iterable; and only if it is iterable, call Promise.all .

If the apolloFetch returns something other than an iterable, you need to decide how your code should behave. Currently it raises an error, which evidently is not what you want; but you need to decide what you do want in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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