即使我只有一个参数,我也收到了“Wrapped promise not iterable 错误" [英] I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

查看:18
本文介绍了即使我只有一个参数,我也收到了“Wrapped promise not iterable 错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 node.js 微服务中使用 Promise.all.Promise.all 的目的是遍历(查询)数组中的所有元素,并通过 apolloFetch 调用另一个微服务,然后在数据库中执行这些查询,然后返回成功或错误.我收到了一个Wrapped promise is not iterable"错误——我检查了一些关于 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.

这篇关于即使我只有一个参数,我也收到了“Wrapped promise not iterable 错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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