Skip to content

TypeError: Cannot read properties of undefined (reading 'toString') #1707

@Cooya

Description

@Cooya

Environment:

  • Node.js Version: 16.13.0
  • Redis Server Version: 4.0.9
  • Node Redis Version: 4.0.0-rc.3
  • Platform: Ubuntu 18

When using ttl in legacy mode, there is an error. To reproduce :

const redis = require('redis');

(async () => {
	const redisClient = redis.createClient({ legacyMode: true });
	await redisClient.connect();

	await redisClient.set('key', 'value', 'EX', 100, (err, res) => console.log(err, res));
})();

The error is

TypeError: Cannot read properties of undefined (reading 'toString')
    at encodeCommand (/home/nicolas/Projects/mi/node_modules/redis/dist/lib/commander.js:72:30)
    at encodeCommand.next (<anonymous>)
    at RedisClient._RedisClient_tick (/home/nicolas/Projects/mi/node_modules/redis/dist/lib/client/index.js:430:20)
    at RedisClient._RedisClient_sendCommand (/home/nicolas/Projects/mi/node_modules/redis/dist/lib/client/index.js:410:82)
    at RedisClient.sendCommand (/home/nicolas/Projects/mi/node_modules/redis/dist/lib/client/index.js:361:93)
    at RedisClient.<computed> [as set] (/home/nicolas/Projects/mi/node_modules/redis/dist/lib/client/index.js:395:14)
    at /home/nicolas/Projects/mi/workshop/test_redis.js:7:20

The is due to the fact that redis v4 wants a string as ttl but redis v3 provides a number. I think both should be supported.

To fix the issue, something like this would be nice :

function* encodeCommand(args) {
    yield `*${args.length}${DELIMITER}`;
    for (let arg of args) {
        if(typeof arg === 'number')
           arg = String(arg);
        const byteLength = typeof arg === 'string' ? Buffer.byteLength(arg) : arg.length;
        yield `$${byteLength.toString()}${DELIMITER}`;
        yield arg;
        yield DELIMITER;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions