1

I have a pretty complicated node.js microservice that utilizes graphql as well, to first query a database, then populate an array with the returning values, and then run a graphql mutation inside an await and Promise.all call. With the following code I keep getting an error: 'await has to be used inside an async function' - but, I already have everything wrapped inside an asyn function! Can someone point out what I'm doing wrong? (PS: there might be other errors as well that I'm not seeing because I can't get past the first error)

Note: this question is not a duplicate - the other question had a problem with the function name - in my function the name is resolve() - there is no additional function name.

  const QryAllBooks = {
    type: new GraphQLList(BookType),
    args: {},
    async 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 apolloFetch  
          //goes in sequence of bookid, one after the other

         await Promise.all(Object.values(input).map(async (promises, i) => {
                  apolloFetch({ promises[i]});
                  })
                 .catch(( e ) => {
                     errorLogger( 29, 'Error', e );
                 )};
                  });
            });
        }
      };

     module.exports = {
          QryAllBooks,
          BookType
     };
Roger Dodger
  • 927
  • 2
  • 16
  • 37
  • `pool.query(sql, (err, results) => {` is not `async`. You only need to use `async` when there's an `await` directly inside the function, best to use normal functions otherwise – CertainPerformance Feb 27 '19 at 00:25
  • CertainPerformance - it is not a duplicate - the other question had a problem with the function name - in my function the name is resolve() - there is no additional function name. – Roger Dodger Feb 27 '19 at 00:43
  • It's the same thing - in that question, OP was trying to use `await` in a non-async function (there, named `start`), just like you're doing here (with an inline arrow function) – CertainPerformance Feb 27 '19 at 00:45
  • CertainPerformance - I'm confused - if you notice, I have everything wrapped inside async resolve(){....} – Roger Dodger Feb 27 '19 at 00:51
  • No, like I said in the first comment, `pool.query(sql, (err, results) => {` is not `async`. The function immediately containing the `await` must be `async`. – CertainPerformance Feb 27 '19 at 00:52
  • OK - I added async to pool.query but that throws an error: "unexpected identified" and points to pool. – Roger Dodger Feb 27 '19 at 00:55
  • Sounds like you didn't put it in the right place, use `async (err, results) =>` – CertainPerformance Feb 27 '19 at 01:44
  • I tried an alternate way but am having some issues - I posted the question here: https://stackoverflow.com/questions/54896838/im-getting-a-wrapped-promise-not-iterable-error-even-though-i-have-only-one-p – Roger Dodger Feb 27 '19 at 01:54

0 Answers0