我得到一个'包裹的承诺不可迭代的错误',尽管我只有一个参数[英] I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

本文是小编为大家收集整理的关于我得到一个'包裹的承诺不可迭代的错误',尽管我只有一个参数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试在我的node.js microservice中使用Promise.l.承诺的目的.所有内容是要浏览(查询)数组中的所有元素,并通过Apollofetch调用另一个微服务,然后在数据库中执行这些查询,然后返回成功或错误.我得到的"包装承诺是不可能的"错误 - 我检查了一些帖子,因此有类似的错误,但是在所有情况下,都有2个参数,而我只通过一个 - 使用apollofetch来连接到另一个微服务,该微服务(在数组中)将每个查询(在数组中)连接,然后在数据库上执行一些操作.

有人可以在这里弄清楚我做错了什么:

     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.

我认为:

的答案

有人可以弄清楚我在这里做错了什么

是,当apolloFetch返回与估计收藏不同的东西时,您不允许使用.

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

如果apolloFetch返回了一个以外的东西,则需要确定代码应如何行为.目前,它引起了一个错误,显然不是您想要的.但是您需要在这种情况下决定自己想要什么.

本文地址:https://www.itbaoku.cn/post/1938109.html

问题描述

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.