-
-
Save evantahler/0c59aa8680259aee3e01 to your computer and use it in GitHub Desktop.
exports.default = { | |
mongo: function(api){ | |
return { | |
enable: true, | |
host: '1.2.3.4' | |
port: 27017 | |
db: 'myDatabase' | |
} | |
} | |
} |
var mongoPackage = require('mongodb'); | |
var mongo = function (api, next) { | |
api.mongo = {}; | |
api.mongo.client = {}; | |
api.mongo.enable = api.config.mongo.enable; | |
api.mongo.client = mongoPackage.MongoClient; | |
api.log("configuring MongoDB "+api.config.mongo.enable, "notice"); | |
if (api.config.mongo.enable == true) { | |
var url = 'mongodb://'+api.config.mongo.host+':'+api.config.mongo.port+'/'+api.config.mongo.db; | |
api.mongo.client.connect(url, function(err, db) { | |
if(err) { | |
api.log(err+"error in mongoDB connection", "notice"); | |
next(); | |
} else { | |
api.log("mongoDB connection ok ", "notice"); | |
api.mongo.db = db; | |
next(); | |
} | |
}); | |
} | |
} | |
///////////////////////////////////////////////////////////////////// | |
// exports | |
exports.mongo = mongo; |
my action is like this
'use strict'
exports.testmongo = {
name: 'testmongo',
description: 'Creates a new user account',
blockedConnectionTypes: [],
outputExample: {},
version: 1.0,
toDocument: true,
run: function(api, data, next) {
console.log("sumit1");
const user={"name":"ajay"};
api.mongo.db.c1.insert(user);
next();
} //end of run
};//end of route
with following error -
/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/actions/testmongo.js:15
api.mongo.c1.insert(user);
^
TypeError: Cannot read property 'c1' of undefined
at Object.run (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/actions/testmongo.js:15:26)
at preProcessAction (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/actionhero/initializers/actionProcessor.js:278:31)
at /Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:3694:9
at /Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:359:16
at replenish (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:876:25)
at /Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:885:9
at eachOfLimit (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:912:22)
at /Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:917:16
at _parallel (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:3685:5)
at Object.series (/Users/nankisinghchadha/nodejslearningbysumit/actionmongodb/node_modules/async/dist/async.js:4525:3)
If connection parameters are already defined in config/mongo-config.js, then why whould it be defined again in action ?