我在生产环境中遇到以下错误我在staging和dev环境中测试了这段代码,但它只在生产环境中导致错误
#node /home/abc/xyz/current/packages/omi-worker/bulk_load_devices.js
Error:
There is error while connecting to mongo client
/home/abc/xyz/releases/20170630032343/packages/omi-worker/node_modules/mongodb/lib/mongo_client.js:456
throw err
^
TypeError: db.collection is not a function
at /abc/xyz/viewpoint/releases/20170630032343/packages/omi-worker/src/device.js:71:14
at nextTask (/home/abc/xyz/releases/20170630032343/packages/omi worker/node_modules/async/dist/async.js:5297:14)
at next (/home/abc/xyz/releases/20170630032343/packages/omi-worker/node_modules/async/dist/async.js:5304:9)
at /home/abc/xyz/releases/20170630032343/packages/omi-worker/node_modules/async/dist/async.js:906:16
at /abc/xyz/viewpoint/releases/20170630032343/packages/omi-worker/src/device.js:63:15
at connectCallback (/home/abc/xyz/releases/20170630032343/packages/omi-worker/node_modules/mongodb/lib/mongo_client.js:505:5)
at /home/abc/xyz/releases/20170630032343/packages/omi-worker/node_modules/mongodb/lib/mongo_client.js:453:13
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
以下是节点脚本片段,它以临时哈希处理一些数据并创建mongodb连接并将数据插入数据库
#!/usr/bin/env node
var MongoClient = require('mongodb').MongoClient;
var service = require('./service');
var service_1 = require('./service_1');
var service_2 = require('./service_2');
var database = require('./../utility/database');
var fs = require('fs');
var moment = require('moment');
var GLOBAL = require("./../config/global");
var _ = require('lodash');
var async = require('async');
var log4js = require('log4js');
var path = require('path');
var logFilepath = path.join(__dirname, './../config/log4js.json');
var logConfig = fs.readFileSync(logFilepath);
log4js.configure(logConfig);
var logger = log4js.getLogger('bulk_load_devices');
var mongo_server = GLOBAL.MONGO_SERVER.url;
module.exports = {
bulkLoad: function() {
async.waterfall(
[
// Update devices details from service_1
function(callback) {
var big_bacon = service_1.bulkLoad(data);
callback(null, big_bacon);
},
function(big_bacon, callback) {
var commands = service_1.update_command_table(big_bacon);
callback(null, big_bacon, commands);
},
// Update devices details from service_2
function(big_bacon, commands, callback) {
big_bacon = service_2.bulkLoad(big_bacon);
callback(null, big_bacon, commands);
},
// Connect to mongoclient for database operations.
function(big_bacon, commands, callback) {
MongoClient.connect(mongo_server, function(err, db) {
if (err) {
logger.debug('There is error while connecting to mongo client');
console.log('There is error while connecting to mongo client');
callback(null);
} else {
callback(null, db, big_bacon, commands);
}
});
},
// Bulk insert process commands data into temporary command worker.
function(db, big_bacon, commands, callback) {
console.log("collection======" + db.collection);
db.collection(GLOBAL.COMMAND_WORKER).insertMany(commands, function(err, r) {
callback(null, db, big_bacon);
})
}
] ],
function(err, result) {}
);
},
prepareData: function() {
var processData = [];
var deviceData = service.getDeviceList();
async.each(deviceData, function(device, callback) {
var currentTime = moment(moment()).format('YYYY-MM-DD HH:mm Z');
record = {
guid: device['properties']['guid'],
};
processData.push(record);
});
return processData;
}
}
答案 0 :(得分:0)
您的代码不正确。
async.waterfall([
...
// Connect to mongoclient for database operations.
function(big_bacon, commands, callback) {
MongoClient.connect(mongo_server, function(err, db) {
if (err) {
logger.debug('There is error while connecting to mongo client');
console.log('There is error while connecting to mongo client');
callback(null); // <<<<<<<
} else {
callback(null, db, big_bacon, commands);
}
});
},
// Bulk insert process commands data into temporary command worker.
function(db, big_bacon, commands, callback) {
console.log("collection======" + db.collection);
db.collection(GLOBAL.COMMAND_WORKER).insertMany(commands, function(err, r) {
callback(null, db, big_bacon);
})
}
],
如果您无法连接到Mongo,则db
- 对象未定义,并且以下步骤无关紧要。您必须致电callback(err)
而不是callback(null)
来打破async.waterfall
并跳转到最终回调。
], // end of waterfall array function
function(err, result) {
if (err) {
// process error here
}
}
); // end of async.waterfall