node.js - 使 Node.js Redis 客户端的 .multi() 和 .batch() 在结果中返回错误以用于测试目的

标签 node.js redis node-redis

我可以在 docs 中阅读来自 Node.js Redis 客户端:

If your code contains an syntax error an EXECABORT error is going to be thrown and all commands are going to be aborted. That error contains a .errors property that contains the concret errors. If all commands were queued successfully and an error is thrown by redis while processing the commands that error is going to be returned in the result array! No other command is going to be aborted though than the onces failing.

如何编写 Node.js Redis 客户端的 .multi().batch() 以便在结果中出现错误以进行测试?

最佳答案

不确定它是否仍然与您相关,但以下代码会为 MULTI 产生 EXECABORT 错误。 附言。我不是 nodejs 开发人员 :)

代码:

var redis = require("redis"),
client = redis.createClient();
client.sadd("bigset", "a member");
client.sadd("bigset", "another member");
set_size = 20;

while (set_size > 0) {
    client.sadd("bigset", "member " + set_size);
    set_size -= 1;
}

// multi chain with an individual callback
client.multi()
    .scard("bigset")
    .smembers("bigset")
    .set("a")
    .keys("*", function (err, replies) {
        // NOTE: code in this callback is NOT atomic
        // this only happens after the the .exec call finishes.
        client.mget(replies, redis.print);
    })
    .dbsize()
    .exec(function (err, replies) {
//        console.log("MULTI got " + replies.length + " replies");
  //      replies.forEach(function (reply, index) {
//            console.log("Reply " + index + ": " + reply.toString());
    //    });
        console.log(replies);
        console.log(err);
        client.quit();
    });

输出:

undefined

{ [ReplyError: EXECABORT Transaction discarded because of previous errors.] command: 'EXEC', code: 'EXECABORT', errors: [ { [ReplyError: ERR wrong number of arguments for 'set' command] command: 'SET', args: [Object], code: 'ERR', position: 2 } ] }

说明:SET 命令有两个参数。我只给了一个并将错误打印到控制台。

有关该主题的详细说明在 this 中问题。请参阅 BridgeAR 的最后一条评论。

示例代码取自 nodejs git repo

关于node.js - 使 Node.js Redis 客户端的 .multi() 和 .batch() 在结果中返回错误以用于测试目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43216644/

相关文章:

PHP Redis session 处理程序多用户

node.js - node_redis : using client. forEach 内多

javascript - Redis 命令都是异步的吗?

javascript - 如何检测客户端与nodejs中的redis断开连接?

node.js - 在 NodeJS 中存储服务器端 cookie 的位置?

javascript - 是否可以拦截 Nest.js 中的提供者?

facebook - Heroku + Facebook + NodeJS : cannot find module 'OAuth'

node.js - 如何衡量 NodeJS 中请求花费的时间

javascript - 如何在 Node.js 的 console.log() 中创建换行符

redis - ServiceStack 消息 API : Can it make a broadcast?