json - node-redis 错误 : Deprecated: The SET command contains a argument of type Object

标签 json node.js encryption redis

我正在使用 connect-redis-crypto 模块(https://github.com/jas-/connect-redis-crypto ) 是为在 connect-redis( https://github.com/tj/connect-redis ) 之上加密 redis session 数据而构建的。我的redis版本是3.2.8

我遇到错误 node-redis: Deprecated: The SET command contains a argument of type Object。根据较大的错误消息,它似乎来自尝试解析字符串 [object Object] 而不是 JSON 字符串。我将保存用户信息的嵌套对象放在 req.session 上,它直接存储(最好是加密)在 redis 中。

我从一些来源了解到 Redis 中不允许嵌套对象,这可能会导致此错误,但我相信该库将数据存储为 JSON 以允许嵌套对象。 connect-redis 对我来说工作正常,但是当这个 connect-redis-crypto 库尝试 JSON 解析加密数据时,它会抛出这个特殊错误。

非常感谢您的帮助!

node_redis: Deprecated: The SET command contains a argument of type Object.
This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0 on.
Please handle this in your code to make sure everything works as you intended it to.
8 May 18:24:48 - ---NEW REQUEST---
REQUEST : GET /api/somePath/client
QUERY   : {}
BODY    : {}
data [object Object]
err SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Command.callback (/Users/Documents/web-AOT/server/node_modules/connect-redis-crypto/lib/connect-redis.js:262:35)
    at normal_reply (/Users/Documents/web-AOT/server/node_modules/redis/index.js:721:21)
    at RedisClient.return_reply (/Users/Documents/web-AOT/server/node_modules/redis/index.js:819:9)
    at JavascriptRedisParser.returnReply (/Users/Documents/web-AOT/server/node_modules/redis/index.js:192:18)
    at JavascriptRedisParser.execute (/Users/Documents/web-AOT/server/node_modules/redis-parser/lib/parser.js:560:12)
    at Socket.<anonymous> (/Users/Documents/web-AOT/server/node_modules/redis/index.js:274:27)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)

最佳答案

The solution is here

您必须将您的对象包装在 JSON.stringify 中,然后记得在稍后查询 key 时 JSON.parse 响应。

// set
client.set(
  "key",
  JSON.stringify(
    {
      example: {
        field: "testing",
        field1: 333
      },
      field: 123
    }, () => {}
  )
);

// get
client.get("key", (err, data) => {
  console.log(JSON.parse(data));
});

关于json - node-redis 错误 : Deprecated: The SET command contains a argument of type Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43858414/

相关文章:

json - 用于验证要导入 Firebase 的 JSON 的工具

javascript - 全局 npm 包在 Ubuntu 上的安装位置

php - vba 中的 hmacsha1 加密不等于使用 pack 方法的 php hmac sha1 值

encryption - openssl 命令行来加密 RC4,不是预期的结果,不明白

iphone - 使用带密码的 AES 算法解密图像

android - 为什么 ProgressDialog 在我们运行时没有显示出来?

json - 其余 : Sending List of Enums does not send all data

python - 将字典列表传递给接受 json 文件的函数

node.js - 在 GraphQL Playground 中设置我想要使用的测试 header 会导致 "Server cannot be reached"

javascript - 如何在node.js中重用WriteStream的内容