我正在尝试使用以下代码连接到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'"}
任何想法,这可能是错的?
答案 0 :(得分:0)
我通过在app.js文件顶部添加以下行来解决问题:
Object.assign = require('object-assign')
参考链接:Object function Object() { [native code] } has no method 'assign' #120