gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:28:20 29 4
gpt4 key购买 nike

我可以在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com