Openshift - 连接到 mongodb 时出现内部错误
Openshift - InternalError while connecting to mongodb
我正在尝试使用以下代码连接到 mongodb:
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);
});
}
它在这一行之前工作正常:console.log('~~ Before firing query to accounts');
。之后它在浏览器中显示此错误:
{"code":"InternalError","message":"Object function Object() { [native code] } has no method 'assign'"}
有什么想法,这里有什么问题吗?
我通过在 app.js 文件顶部添加以下行解决了这个问题:
Object.assign = require('object-assign')
参考link:Object function Object() { [native code] } has no method 'assign' #120
我正在尝试使用以下代码连接到 mongodb:
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);
});
}
它在这一行之前工作正常:console.log('~~ Before firing query to accounts');
。之后它在浏览器中显示此错误:
{"code":"InternalError","message":"Object function Object() { [native code] } has no method 'assign'"}
有什么想法,这里有什么问题吗?
我通过在 app.js 文件顶部添加以下行解决了这个问题:
Object.assign = require('object-assign')
参考link:Object function Object() { [native code] } has no method 'assign' #120