简体   繁体   English

即使只有一个参数,我也会收到“包装的保证不是可迭代的错误”

[英]I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter

I'm trying to use a Promise.all in my node.js microservice. 我试图在我的node.js微服务中使用Promise.all。 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. Promise.all的目的是遍历(查询的)数组中的所有元素,并通过apolloFetch调用另一个微服务,该微服务然后在数据库中执行那些查询,然后返回成功或错误。 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. 我收到“包装的承诺不是不可迭代的”错误-我在SO上检查了几处类似错误的帖子,但在所有这些情况下,都传递了2个参数,而我只传递了一个参数-除了我使用apolloFetch连接到另一个微服务,该微服务接受每个查询(在数组中),然后对数据库执行一些操作。

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 . 该代码从调用apolloFetch获取返回值,并将其无条件地馈送到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. 是的,您不允许apolloFetch返回与可迭代集合不同的情况。

Instead, call apolloFetch , figure out whether or not the return value is iterable; 相反,调用apolloFetch ,搞清楚返回值是否为迭代; and only if it is iterable, call Promise.all . 并且仅可迭代时,才调用Promise.all

If the apolloFetch returns something other than an iterable, you need to decide how your code should behave. 如果apolloFetch返回的不是可迭代的内容,则需要确定代码的行为方式。 Currently it raises an error, which evidently is not what you want; 当前它会引发错误,这显然不是您想要的; but you need to decide what you do want in that case. 但在这种情况下,您需要决定要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code 即使我定义了一个字符串,我也收到了一个错误,即使我定义了一个字符串,我也收到了一个错误 - I'm getting an error even though I defined a string and I'm getting an error even though I defined a string Sinon存根错误:即使我正在还原,也“尝试包装已经包装的帖子” - Sinon stub error: “Attempted to wrap post which is already wrapped” even though I'm restoring 我有一个错误:Uncaught (in promise) TypeError: source is not async iterable - I'm having an error: Uncaught (in promise) TypeError: source is not async iterable 即使我提供数据,为什么我会收到验证错误? - Why am I getting a validation error even though I'm providing data? 我有 2 个状态,但即使我只 setState 其中一个,它们都在更新 - I have 2 states, but both are updating even though I only setState one of them 在控制台中获取未定义的错误通知,即使我正在检查未定义 - Getting undefined error notice in console even though I'm checking for undefined 即使我的 package.json 和 node_modules 文件夹中有它们,我也得到了未定义的包 - I'm getting undefined packages even though I have them in my package.json and node_modules folder 未处理的承诺拒绝,即使我很确定我已经处理了它们 - Unhandled promise rejection, even though I'm pretty sure I've handled them all 使用redmine API进行POST请求时出现401错误,即使我已经包含了api密钥 - Getting 401 error when using redmine API for a POST request even though I have included the api key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM