Devarshi Devarshi - 4 months ago 16
Node.js Question

Openshift - InternalError while connecting to mongodb

I am trying to connect to mongodb using below code:

var restify = require('restify');
var mongojs = require('mongojs');

var ip_addr = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

console.log('ip_addr: ' + ip_addr + ' port ' + port);

var server = restify.createServer({name: 'myproject'});
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS());

server.listen(port, ip_addr, function(){
console.log(server.name + ' listening at url ' + server.url);
});

var connectionString = '127.0.0.1:27017/myproject';

if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
connectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ':' +
process.env.OPENSHIFT_MONGODB_DB_PASSWORD + '@' +
process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}

console.log('~~ connectionString: '+connectionString);

var db = mongojs(connectionString, ['accounts']);
var accounts = db.collection('accounts');
console.log('~~ db: '+ db + ' accounts:' + accounts);

var ACNT_PATH = '/accounts';

// get all records
server.get({path: ACNT_PATH, version:'0.0.1'}, findAllAccounts);

// defining call backs
function findAllAccounts(req, res, next){
res.setHeader('Access-Control-Allow-Origin', '*');
console.log('~~ Before firing query to accounts');

db.accounts.find(function(err, docs){

if(docs){
console.log('About to return docs');
res.send(200, docs);
return next();
}

console.log('about to return error');
return next(err);
});
}


It is working fine till this line:
console.log('~~ Before firing query to accounts');
. After which it shows this error in browser:

{"code":"InternalError","message":"Object function Object() { [native code] } has no method 'assign'"}


Any ideas, what could be wrong over here?

Answer

I solved the problem by adding below line at top of app.js file:

Object.assign = require('object-assign')

Reference link: Object function Object() { [native code] } has no method 'assign' #120

Comments