0

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
   };
Roger Dodger
  • 927
  • 2
  • 16
  • 37

1 Answers1

1

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.

bignose
  • 30,281
  • 14
  • 77
  • 110
  • Ok so I got past the error and used the map function to separate each item in the array which is essentially a query, but now I'm getting an error that the somehow the query is not being passed. I updated the code above to reflect what I tried. – Roger Dodger Feb 27 '19 at 04:13
  • @RogerDodger, thanks for reporting that this is the answer. Can you mark this answer as correct? Your separate question should be asked as a new question, with the context of what readers need to understand that new question. – bignose Feb 27 '19 at 04:45
  • bignose - That is not the answer - I got past one error but now I'm getting an empty value in 'p' that is passed as a parameter for apolloFetch call. I don't even know if I'm on the right track since I don't have confirmation that my way of doing the apolloFetch call is even legit. If I accept it as the answer, what would be the next steps to figure out and solve my problem? – Roger Dodger Feb 27 '19 at 04:51
  • 1
    @RogerDodger, it's normal for one question to lead to new questions. The way we deal with that is to leave the original (with answers marked correct, if they answer the original question correctly) and then create new questions as needed. – bignose Feb 27 '19 at 05:38
  • I did as per your suggestion and created another follow up question here (and I won't be marking it that as answered unless I get a working solution): https://stackoverflow.com/questions/54908549/why-is-my-apollofetch-call-returning-an-empty-query-when-called-from-within-a-pr – Roger Dodger Feb 27 '19 at 15:15
  • Thanks, glad you got it sorted :-) – bignose Feb 27 '19 at 22:13