我开发了一个Nodejs REST搜索API,用蓝鸟承诺API从mongodb返回数据。此API在本地工作,但在Heroku中失败,错误如下。
顺便说一句,我在Heroku中有其他REST API正常工作。我想这可能与env /配置有关。请详细说明一下。
TypeError: db.collection(...).findAsync is not a function
at next (/app/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request]
(/app/node_modules/express/lib/router/layer.js:95:5)
at param (/app/node_modules/express/lib/router/index.js:365:14)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/app/node_modules/express/lib/router/index.js:174:3)
at /app/node_modules/express/lib/router/index.js:281:22
at router (/app/node_modules/express/lib/router/index.js:47:12)
at param (/app/node_modules/express/lib/router/index.js:354:14)
at Layer.handle [as handle_request]
(/app/node_modules/express/lib/router/layer.js:95:5)
at Function.process_params
(/app/node_modules/express/lib/router/index.js:410:3)
at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
at /app/node_modules/express/lib/router/index.js:284:7
at Layer.handle [as handle_request]
(/app/node_modules/express/lib/router/layer.js:95:5)
at Function.process_params
(/app/node_modules/express/lib/router/index.js:335:12)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
at /app/node_modules/express-fileupload/lib/index.js:18:14
at /app/node_modules/express/lib/router/index.js:284:7
code is below:
const mongoose = require('../../../utils/mongodb');
const Promise = require('bluebird');
const mongoDB = require('mongodb');
Promise.promisifyAll(mongoDB);
const db = mongoose.connection.db;
exports.searchChat = function (req, res) {
const userId = req.params.userid;
const getChatArray = function () {
return db.collection('chats').findAsync({ participants: {
$elemMatch: { $eq: userId } } }).then(chatCursor =>
chatCursor.toArrayAsync());};
const promises = getChatArray().map(chat =>
db.collection('messages').findAsync({ chat: { $eq: chat._id }
}).then(msgCursor => msgCursor.toArrayAsync()));
Promise.all(promises).then((messages) =>{res.json(messages); }).catch(
reason => chatlog.error(`retrieve chat error: ${reason}`));
};