【发布时间】:2019-06-08 10:49:21
【问题描述】:
您好,我正在尝试使用 ioredis 将 JSON 存储在 redis 中。这个 JSON 也包含一个函数。我的json的结构是这样的:
var object = {
site1: {
active: true,
config1:{
// Some config in JSON format
},
config2: {
// Some other config in JSON format
},
determineConfig: function(condition){
if(condition) {
return 'config1';
}
return 'config2';
}
}
}
我正在使用 IOredis 将这个 json 存储在 redis 中:
redisClient.set(PLUGIN_CONFIG_REDIS_KEY, pluginData.pluginData, function (err, response) {
if (!err) {
redisClient.set("somekey", JSON.stringify(object), function (err, response) {
if (!err) {
res.json({message: response});
}
});
}
});
当我这样做时,determineConfig 键会从 object 中截断,因为如果类型是函数,JSON.stringify 会删除它。有什么方法可以将这个函数存储在 redis 中,并在我从 redis 取回数据后执行。我不想将函数存储为字符串,然后使用eval 或new Function 进行评估。
【问题讨论】:
标签: node.js json redis ioredis