var mongo = require("mongodb").MongoClient;
mongo.connect("mongodb://localhost:27017/testdb", function(err, db){
var printHelloWorld = function(){
console.log("Hello World!");
}
var testData = {_id: "testData", data: printHelloWorld};
db.collection("test").insertOne(testData, function(err, result){
console.log("added data");
db.collection("test").find({_id: "test data"}).toArray(function(err, docs){ docs[0].printHelloWorld();
});
});
});
TypeError: Cannot read property 'printHelloWorld' of undefined at /home/ubuntu/workspace/test2.js:16:20 at handleCallback (/home/ubuntu/workspace/node_modules/mongodb/lib/utils.js:96:12) at /home/ubuntu/workspace/node_modules/mongodb/lib/cursor.js:835:16 at handleCallback (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:159:5) at setCursorNotified (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:490:3) at /home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:561:16 at queryCallback (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:241:5) at Callbacks.emit (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3) at null.messageHandler (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:295:23) at Socket.<anonymous> (/home/ubuntu/workspace/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:285:22)
You can although MongoDB don't seem very keen to encourage it...
I've already looked at this but it doesn't seem to work either, if I perform an identical operation but change the collection to system.js then I get the same error.
TypeError: Cannot read property 'js' of undefined at /home/ubuntu/workspace/test2.js:5:14 at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:519:11 at nextTickCallbackWith0Args (node.js:420:9) at process._tickCallback (node.js:349:13)
You can although MongoDB don't seem very keen to encourage it...
I am looking to retrieve functions and then run them in node.js instead.
After retrieval is there a way to easily convert from the code object back to a function that can be run?
Hi Adam,
Generally, it is not recommended to dynamically evaluate code at runtime as it could be a potential security risk and performance issue.
Although if you have a good use case for it, you could store the function string into the database and construct later using Function() class. Using your code example:
var testData = {_id: "testData", data: "console.log('Hello World')"};
db.collection("test").insertOne(testData, function(err, result){
console.log("added data");
db.collection("test").findOne({_id: "testData"}, function(err, doc){
func = new Function(doc.data);
func();
});
});
Please see Function constructor to find out more.
You may also be interested to know that the next session for free online course M101JS: MongoDB for Node.js Developers is starting in 24th May.
Best regards,
Wan.