You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node Redis 4.5.1中ZADD报错TypeError: Cannot read properties of undefined (reading 'toString')

问题原因

Node Redis 4.x版本的zAdd方法参数格式与旧版本(如3.x)不兼容。你当前使用的(key, timestamp, msgid)参数顺序是旧版本写法,但4.x要求第二个参数必须是包含成员对象的数组,或单个{score, value}结构的对象。错误的参数格式导致客户端内部解析时,将时间戳误当成成员列表处理,进而触发Cannot read properties of undefined (reading 'toString')错误。

Redis有序集合不存在时,zAdd会自动创建,因此键不存在并非报错根源。报错堆栈中的transformNumberInfinityArgument函数是客户端用于解析+inf/-inf这类特殊分数的逻辑,因参数格式错误接收到undefined值,才引发toString()调用失败。

解决方法

将zAdd调用修改为4.x要求的格式,以下两种写法均可:

写法一:成员对象数组(推荐,支持批量添加)

let msgid = String(feedResult._id);
console.log("222", feedKey, msgid, typeof msgid);
await redisClient.zAdd(`user:messages:${userId}`, [
  {
    score: new Date(feedResult.createdAt).getTime(),
    value: msgid
  }
]);

写法二:单个成员对象(适合单次添加一个成员)

let msgid = String(feedResult._id);
console.log("222", feedKey, msgid, typeof msgid);
await redisClient.zAdd(`user:messages:${userId}`, {
  score: new Date(feedResult.createdAt).getTime(),
  value: msgid
});

内容的提问来源于stack exchange,提问作者user824624

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 06:40:29