我有一个存储在compose.io mongo服务器上的javascript函数echoFunction();
。从shell开始,我可以在运行db.loadServerScripts();
后成功运行该函数,因为运行db.eval( "echoFunction( 'test' )" )
会给我一个未经授权的错误。我的问题是如何使用MongoClient从nodejs中运行这些。从节点我得到以下错误。
TypeError:对象#没有方法' loadServerScripts'
这是我的代码。
app.post('/import', function(req, res) {
res.send(200, "OK ");
var MongoClient = require('mongodb').MongoClient,
format = require('util').format;
MongoClient.connect('mongodb://username:password@compose.io.com/db', function(err, db) {
if (err) throw err;
db.collection('collection').insert(req.body, {
w: 1
}, function(err, objects) {
db.loadServerScripts("system.js");
db.echoFunction();
if (err) console.warn(err.message);
if (err && err.message.indexOf('E11000 ') !== -1) {
// this _id was already inserted in the database
}
});
});
});
由于