【发布时间】:2017-05-07 12:26:12
【问题描述】:
我正在尝试使用来自 Ebay API 的 2 项服务:
findItemsByKeywords(keyword):返回项目列表(对象);
getItemTransactions(itemId):返回项目的交易列表(对象);
在调用 findItemsByKeywords 并获取返回的项目列表后,我需要调用 getItemTransactions 并获取每个项目的交易并使用它们创建一个新列表。
我正在使用 Node.js、Express、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);
}
);
});
}
【问题讨论】:
-
你的依赖是什么?我经常因为包含像 Mongoose 这样的包而没有实际启动 mongodb 服务器而出现此错误。所以要么注释掉/删除包,要么启动mongodb
-
@CodyGuldner var express = require('express'), router = express.Router(), mongoose = require('mongoose'), Article = mongoose.model('Article'); var ebay = require('ebay-api');
-
请确保您的 mongo 客户端已启动,否则您将收到错误消息
标签: javascript node.js mongodb express ejs