[英]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.