Javascript &引用;评估“;存储在MongoDB服务器中的函数

Javascript &引用;评估“;存储在MongoDB服务器中的函数,javascript,node.js,mongodb,server,client,Javascript,Node.js,Mongodb,Server,Client,我的收藏如下: > db.projects_columns.find() { "_id" : "5b28866a13311e44a82e4b8d", "checkbox" : true } { "_id" : "5b28866a13311e44a82e4b8e", "field" : "_id", "title" : "ID", "sortable" : true } { "_id" : "5b28866a13311e44a82e4b8f", "field" : "Project", "t

我的收藏如下:

> db.projects_columns.find()
{ "_id" : "5b28866a13311e44a82e4b8d", "checkbox" : true }
{ "_id" : "5b28866a13311e44a82e4b8e", "field" : "_id", "title" : "ID", "sortable" : true }
{ "_id" : "5b28866a13311e44a82e4b8f", "field" : "Project", "title" : "Project", "editable" : { "title" : "Project", "placeholder" : "Project" } }
{ "_id" : "5b28866a13311e44a82e4b90", "field" : "Owner", "title" : "Owner", "editable" : { "title" : "Owner", "placeholder" : "Owner" } }
{ "_id" : "5b28866a13311e44a82e4b91", "field" : "Name #1", "title" : "Name #1", "editable" : { "title" : "Name #1", "placeholder" : "Name #1" } }
{ "_id" : "5b28866a13311e44a82e4b92", "field" : "Name #2", "title" : "Name #2", "editable" : { "title" : "Name #2", "placeholder" : "Name #2" } }
{ "_id" : "5b28866a13311e44a82e4b93", "field" : "Status", "title" : "Status", "editable" : { "title" : "Status", "type" : "select", "source" : [ { "value" : "", "text" : "Not Selected" }, { "value" : "Not Started", "text" : "Not Started" }, { "value" : "WIP", "text" : "WIP" }, { "value" : "Completed", "text" : "Completed" } ], "display" : "function (value, sourceData) { var colors     = { 0: 'Gray', 1: '#E67C73', 2: '#F6B86B', 3: '#57BB8A' }; var status_ele = $.grep(sourceData, function(ele){ return ele.value == value; });  $(this).text(status_ele[0].text).css('color', colors[value]); }", "showbuttons" : false } }
var myObject = JSON.parse(myJSONString);
// displayParam1 and displayParam2 are the stored names of your parameters for the function
myObject.display = Function(myObject.displayParam1, myObject.displayParam2, myObject.display)
你可以在最后一个文档中看到,我将函数存储为文本。现在的想法是,我将请求这些数据,并将以Javascript数组格式存储

但是我希望我的函数没有引号!您可以看到,简单地计算它是不可行的,因为我需要让它仍然需要在对象内部,以便在使用数组时执行

我怎样才能做到这一点


谢谢你的帮助

>P>有两种可能的解决方案,但都不特别安全,并且应该强烈地考虑为什么首先需要将函数存储为字符串。也就是说,你可以做两件事

最简单的就是使用。要执行此操作,您必须首先像普通一样解析对象,然后设置要作为函数字符串求值结果的属性,如下所示:

// Pass in whatever JSON you want to parse
var myObject = JSON.parse(myJSONString);
// Converts the string to a function
myObject.display = eval("(" + myObject.display + ")");
// Call the function with whatever parameters you want
myObject.display(param1, param2);
附加括号用于确保评估工作正常。请注意,Mozilla认为这是不安全的,并且明确建议不要使用eval

第二个选项是使用构造函数。为此,您需要重新构造数据,以便单独存储参数,这样您就可以执行以下操作:

> db.projects_columns.find()
{ "_id" : "5b28866a13311e44a82e4b8d", "checkbox" : true }
{ "_id" : "5b28866a13311e44a82e4b8e", "field" : "_id", "title" : "ID", "sortable" : true }
{ "_id" : "5b28866a13311e44a82e4b8f", "field" : "Project", "title" : "Project", "editable" : { "title" : "Project", "placeholder" : "Project" } }
{ "_id" : "5b28866a13311e44a82e4b90", "field" : "Owner", "title" : "Owner", "editable" : { "title" : "Owner", "placeholder" : "Owner" } }
{ "_id" : "5b28866a13311e44a82e4b91", "field" : "Name #1", "title" : "Name #1", "editable" : { "title" : "Name #1", "placeholder" : "Name #1" } }
{ "_id" : "5b28866a13311e44a82e4b92", "field" : "Name #2", "title" : "Name #2", "editable" : { "title" : "Name #2", "placeholder" : "Name #2" } }
{ "_id" : "5b28866a13311e44a82e4b93", "field" : "Status", "title" : "Status", "editable" : { "title" : "Status", "type" : "select", "source" : [ { "value" : "", "text" : "Not Selected" }, { "value" : "Not Started", "text" : "Not Started" }, { "value" : "WIP", "text" : "WIP" }, { "value" : "Completed", "text" : "Completed" } ], "display" : "function (value, sourceData) { var colors     = { 0: 'Gray', 1: '#E67C73', 2: '#F6B86B', 3: '#57BB8A' }; var status_ele = $.grep(sourceData, function(ele){ return ele.value == value; });  $(this).text(status_ele[0].text).css('color', colors[value]); }", "showbuttons" : false } }
var myObject = JSON.parse(myJSONString);
// displayParam1 and displayParam2 are the stored names of your parameters for the function
myObject.display = Function(myObject.displayParam1, myObject.displayParam2, myObject.display)

这个方法肯定需要更多的修改,所以如果你想使用你现有的结构,我推荐eval。但是,再次确认这是绝对必要的,因为这两种方法都被认为是不安全的,因为外部参与者基本上可以将代码注入到您的服务器中

谢谢,括号的缺失实际上是造成我问题的原因。但是如果你能告诉我,我需要计算函数的原因是我已经创建了一个表,正如你可能猜到的,它在一个对象参数中接受了一个函数。我希望将其存储在服务器端,以便客户端可以创建该功能的多个版本。如果有任何方法可以让这更安全/更简单,请告诉我!