我得到一个 'Wrapped promise not iterable error' 即使我只有一个参数
I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter
我正在尝试在我的 node.js 微服务中使用 Promise.all。 Promise.all 的目的是遍历(查询的)数组中的所有元素,并通过 apolloFetch 调用另一个微服务,然后在数据库中执行这些查询,然后 returns 返回成功或错误。我收到 'Wrapped promise is not iterable' 错误 - 我检查了一些关于 SO 的帖子,它们有类似的错误,但在所有这些情况下,传递了 2 个参数,而我只传递了一个 - 除了我使用的是 apolloFetch为了连接到另一个 MICROSERVICE,它接受每个查询(在数组中),然后对数据库执行一些操作。
有人可以找出我在这里做错了什么吗:
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
中获取 return 值,并无条件地将其提供给 Promise.all
。
我认为答案是:
Can someone figure out what I'm doing wrong here
是,您不允许 apolloFetch
return 与可迭代集合不同的情况。
相反,调用 apolloFetch
,确定 是否 return 值是可迭代的;只有 if 它是可迭代的,调用 Promise.all
.
如果 apolloFetch
return 不是可迭代对象,您需要决定代码的行为方式。目前它引发了一个错误,这显然不是你想要的;但是你需要决定在那种情况下你想要什么。
我正在尝试在我的 node.js 微服务中使用 Promise.all。 Promise.all 的目的是遍历(查询的)数组中的所有元素,并通过 apolloFetch 调用另一个微服务,然后在数据库中执行这些查询,然后 returns 返回成功或错误。我收到 'Wrapped promise is not iterable' 错误 - 我检查了一些关于 SO 的帖子,它们有类似的错误,但在所有这些情况下,传递了 2 个参数,而我只传递了一个 - 除了我使用的是 apolloFetch为了连接到另一个 MICROSERVICE,它接受每个查询(在数组中),然后对数据库执行一些操作。
有人可以找出我在这里做错了什么吗:
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
中获取 return 值,并无条件地将其提供给 Promise.all
。
我认为答案是:
Can someone figure out what I'm doing wrong here
是,您不允许 apolloFetch
return 与可迭代集合不同的情况。
相反,调用 apolloFetch
,确定 是否 return 值是可迭代的;只有 if 它是可迭代的,调用 Promise.all
.
如果 apolloFetch
return 不是可迭代对象,您需要决定代码的行为方式。目前它引发了一个错误,这显然不是你想要的;但是你需要决定在那种情况下你想要什么。