user824624
user824624

Reputation: 8080

node redis can't work in zadd with TypeError: Cannot read properties of undefined (reading 'toString')

I am working on the node redis 4.5.1 - the latest version.

The code below is simple, feedResult is a mongoose model object, feedResult._id is the objectid, so I transform objectid to string with String(feedResult._id). At last redisclient will add the id and timestamp into the redis set based on the key : 'user:messages:'+userId

let msgid = String(feedResult._id)
console.log("222",feedKey, msgid , typeof msgid);
await redisClient.zAdd('user:messages:'+userId, new Date(feedResult.createdAt).getTime(), msgid);

however the code is running with an error each time, I have no clear mind what goes wrong with my code, at the first beginning I though the error is due to the msgid not a string, but I check it twice that msgid is a string.

Err: TypeError: Cannot read properties of undefined (reading 'toString')
        at transformNumberInfinityArgument (/Users/abc/Documents/code/server/node_modules/@redis/client/dist/lib/commands/generic-transformers.js:51:24)
        at Object.transformArguments (/Users/abc/Documents/code/server/node_modules/@redis/client/dist/lib/commands/ZADD.js:29:78)
        at transformCommandArguments (/Users/abc/Documents/code/server/node_modules/@redis/client/dist/lib/commander.js:71:23)
        at Commander.commandsExecutor (/Users/abc/Documents/code/server/node_modules/@redis/client/dist/lib/client/index.js:176:88)
        at Commander.BaseClass.<computed> [as zAdd] (/Users/abc/Documents/code/server/node_modules/@redis/client/dist/lib/commander.js:8:29)

Now I have no clear clue what goes wrong, except that 'user:messages:'+userId is not existing in the redis db, assuming 'user:messages:'+userId key would be created during the command execution

This is the code where the error happens https://github.com/redis/node-redis/blob/master/packages/client/lib/commands/generic-transformers.ts

export function transformNumberInfinityReply(reply: RedisCommandArgument): number {
        switch (reply.toString()) {
            case '+inf':
                return Infinity;
    
            case '-inf':
                return -Infinity;
    
            default:
                return Number(reply);
        }
    }
    
export function transformNumberInfinityNullReply(reply: RedisCommandArgument | null): number | null {
        if (reply === null) return null;
    
        return transformNumberInfinityReply(reply);
    }

Upvotes: 2

Views: 1196

Answers (1)

Leibale Eidelman
Leibale Eidelman

Reputation: 3194

the client zAdd function expects an object or array of objects with score and value (see here). BTW, if you are using a modern IDE, you can trust the autocomplete, as it uses the package built-in typescript declarations.

This should work:

await redisClient.ZADD(`user:messages:${userId}`, {
  score: new Date(feedResult.createdAt).getTime(),
  value: msgId
});

Upvotes: 4

Related Questions