Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created November 15, 2015 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantahler/0c59aa8680259aee3e01 to your computer and use it in GitHub Desktop.
Save evantahler/0c59aa8680259aee3e01 to your computer and use it in GitHub Desktop.
ActionHero + Mongo
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;
@sumit1317
Copy link

Having set the config and initializer files, how do we make a call to db.collection in actions ?

@evantahler
Copy link
Author

api.mongo.db becomes db in the official example http://mongodb.github.io/node-mongodb-native/2.2/api/

@sumit1317
Copy link

I am not able to get it.

@sumit1317
Copy link

If connection parameters are already defined in config/mongo-config.js, then why whould it be defined again in action ?

@sumit1317
Copy link

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

@sumit1317
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment