即使我只有一个参数,我也收到“包装的保证不是可迭代的错误" [英] I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

查看:62
本文介绍了即使我只有一个参数,我也收到“包装的保证不是可迭代的错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的node.js微服务中使用Promise.all. Promise.all的目的是遍历(查询的)数组中的所有元素,并通过apolloFetch调用另一个微服务,该微服务然后在数据库中执行那些查询,然后返回成功或错误.我收到包装的承诺不是不可迭代的"错误-我在SO上检查了几处类似错误的帖子,但在所有这些情况下,都传递了2个参数,而我只传递了一个参数-除了我使用apolloFetch连接到另一个微服务,该微服务接受每个查询(在数组中),然后对数据库执行一些操作.

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
   };

推荐答案

代码从调用apolloFetch中获取返回值,并将其无条件地馈送到Promise.all.

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

我认为答案是

有人可以找出我在这里做错了吗

Can someone figure out what I'm doing wrong here

是的,当apolloFetch返回与可迭代集合不同的内容时,您是不允许的.

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

相反,调用apolloFetch,找出是否返回值是可迭代的;并且只有如果是可迭代的,请调用Promise.all.

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

如果apolloFetch返回的不是可迭代的内容,则需要确定代码的行为方式.当前,它会引发错误,这显然不是您想要的.但是在这种情况下,您需要决定要做什么.

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.

这篇关于即使我只有一个参数,我也收到“包装的保证不是可迭代的错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆