TypeScript 5.2.2 + Redis 4.6.8问题(类型参数...)

blmhpbnm  于 9个月前  发布在  Redis
关注(0)|答案(1)|浏览(65)

我正在申请一家初创公司,希望在虚拟世界中销售服务和产品。他们给了我一个挑战,我必须:
1.创建一个微服务,客户端可以通过Socket.io连接
1.当连接时,用户应该看到可用的房间,当进入任何房间时,这应该返回所有可用硬币的x,y,z位置。
等等
在这里,我存储的房间与硬币在Redis

const loadRoom = async () => {
  await client.connect();

  const newRoom: Room = {
    id: 0,
    space: generateRandomSpaceWithoutNulls()
  };

  printRoom(newRoom);

  client.set("room", JSON.stringify(newRoom));
};

这是生成下面错误的函数

app.get('/room', async (req, res) => {
    client.get('room', (err: Error | null, reply: string | null) => {
      if (reply !== null) {`your text`
        return res.json(JSON.parse(reply));
      }
    });
  });`

我的问题是:

Argument of type '["room", (err: Error | null, reply: string | null) => Response<any, Record<string, any>, number> | undefined]' is not assignable to parameter of type '[key: RedisCommandArgument] | [options: CommandOptions<ClientCommandOptions>, key: RedisCommandArgument]'.
  Type '["room", (err: Error | null, reply: string | null) => Response<any, Record<string, any>, number> | undefined]' is not assignable to type '[options: CommandOptions<ClientCommandOptions>, key: RedisCommandArgument]'.
    Type at position 0 in source is not compatible with type at position 0 in target.
      Type 'string' is not assignable to type 'CommandOptions<ClientCommandOptions>'.
        Type 'string' is not assignable to type '{ readonly [symbol]: true; }'.
46qrfjad

46qrfjad1#

你正在使用Node Redis 3.x的旧语法,它不支持Promises。Node Redis 4.x改变了这一点,如果你使用npm install redis,你会得到什么。
尝试以下语法:

await client.set('key', 'value');
const value = await client.get('key');

相关问题