Re: Storing a Javascript function in a mongoDB database for retrieval later.

From: Wan Bachtiar <wan.bachtiar@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Wed, 11 May 2016 23:08:33 -0700 (PDT)
Why ads?


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 
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function>. 
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 
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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 
<https://university.mongodb.com/courses/M101JS/about> is starting in 24th 
May.

Best regards, 

Wan. 


-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/eeb392e7-0ac9-4ce4-8bb2-89d4f55a53e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?