我可以在另一个承诺中调用承诺(带循环)吗?

我试图从Ebay API中使用2个服务: findItemsByKeywords(keyword):返回项目列表(对象); getItemTransactions(itemId):返回项目的事务列表(对象);我可以在另一个承诺中调用承诺(带循环)吗?

在致电findItemsByKeywords并获得退货物品清单后,我需要拨打getItemTransactions并获取每个物品的交易,并用它们创建新的清单。

我如何使用Node.js,快速,EJS和MongoDB

我的代码工作只有一个互动。

Error: 

Error: unable to connect to database at mongodb://localhost/simplysell-development

at NativeConnection.<anonymous> (/Users/guilhermefalcao/workspace/simplysell/app.js:11:9)

at emitOne (events.js:96:13)

at NativeConnection.emit (events.js:188:7)

at Db.<anonymous> (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:169:10)

at emitTwo (events.js:106:13)

at Db.emit (events.js:191:7)

at Server.listener (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb/lib/db.js:1786:14)

at emitOne (events.js:96:13)

at Server.emit (events.js:188:7)

at Server.<anonymous> (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb/lib/server.js:274:14)

at emitOne (events.js:96:13)

at Server.emit (events.js:188:7)

at Pool.<anonymous> (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb-core/lib/topologies/server.js:334:12)

at emitOne (events.js:96:13)

at Pool.emit (events.js:188:7)

at Connection.<anonymous> (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb-core/lib/connection/pool.js:270:12)

我并不想访问数据库,我不知道为什么这个错误。

我对代码和英语的素质,我现在我开始后悔。

下面是一些代码:

router.get('/bestsellers', (req, res, next) => { 

var i = 0;

var findItemsByKeywordsPromise = findItemsByKeywords('iphone');

findItemsByKeywordsPromise.then(items => {

var getItemTransactionsPromise = getItemTransactions(items);

return getItemTransactionsPromise;

}).then(test => console.log(test))

});

function getItemTransactions(items) {

return new Promise((resolve, reject) => {

var list = [];

items.forEach(function (item) {

ebay.xmlRequest({

serviceName: 'Trading',

opType: 'GetItemTransactions',

// app/environment

devId: 'xxxxxxx',

certId: 'xxxxxxx',

appId: 'xxxxxxx',

sandbox: false,

// per user

authToken: 'xxxxxxx',

params: {

'ItemID': item.itemId,

'NumberOfDays': 30

}

}, function (error, results) {

list.push(results)

resolve(list);

});

})

});

}

function findItemsByKeywords(keywords) {

return new Promise((resolve, reject) => {

var params = {

keywords: [keywords],

};

ebay.xmlRequest({

serviceName: 'Finding',

opType: 'findItemsByKeywords',

appId: 'xxxx', // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI

params: params,

parser: ebay.parseResponseJson // (default)

},

// gets all the items together in a merged array

function itemsCallback(error, itemsResponse) {

if (error) reject(error);

console.log('findItemsByKeywords called');

var items = itemsResponse.searchResult.item;

console.log('Found', items.length, 'items');

resolve(items);

}

);

});

}

回答:

这是正确的。循环完成之前eBay api返回一个值,所以解析将返回一个不完整的数组(列表)。

function getItemTransactions(items) { 

return new Promise((resolve, reject) => {

var list = [];

items.forEach(function (item) {

ebay.xmlRequest({

serviceName: 'Trading',

opType: 'GetItemTransactions',

// app/environment

devId: 'xxxxxxx',

certId: 'xxxxxxx',

appId: 'xxxxxxx',

sandbox: false,

// per user

authToken: 'xxxxxxx',

params: {

'ItemID': item.itemId,

'NumberOfDays': 30

}

}, function (error, results) {

list.push(results)

if (list.length == items.length) {

resolve(list);

}

});

})

});

}

这是我改变,我解决在列表阵列完成:

if (list.length == items.length) { 

resolve(list);

}

回答:

看着我们无法知道这是为什么蒙戈错误发生的代码。

但回答标题中的答案,函数getItemTransactions是错误的。您为数组中的每个项目调用resolve,但Promises只能解析一次,因此它只会返回其解析的第一个项目。

我将其安排在两个功能:

function getItemTransaction (item) { 

return Promise(function (resolve, reject) {

ebay.xmlRequest({

serviceName: 'Trading',

opType: 'GetItemTransactions',

// app/environment

devId: 'xxxxxxx',

certId: 'xxxxxxx',

appId: 'xxxxxxx',

sandbox: false,

// per user

authToken: 'xxxxxxx',

params: {

'ItemID': item.itemId,

'NumberOfDays': 30

}

}, function (error, results) {

if (error) {

return reject(error);

}

resolve(results);

});

});

}

function getItemTransactions(items) {

var promises = items.map(function(item) {

return getItemTransaction(item);

});

// Promise.all waits for all the promises to get resolved

// and return an array of resolved promises

return Promise.all(promises);

}

第二个功能可能没有必要,如果你在你的主代码操作:

findItemsByKeywords('iphone') 

.then(items => {

return Promise.all(items.map(item => getItemTransaction(item));

})

.then(test => console.log(test));

以上是 我可以在另一个承诺中调用承诺(带循环)吗? 的全部内容, 来源链接: utcz.com/qa/261193.html

回到顶部