我有一个名为personells的数组,其中包含一个名为mgr_wrkpersonnel_empid的密钥。现在,当我循环访问personell时,我需要使用mgr_wrkpersonnel_empid作为我唯一的哈希值来迭代地检索每个personell的整个列表,并将它们推送到一个名为schedule的新数组中,然后在填充每个emp_id的列表时,以某种方式将每个列表Map到其各自的emp_id。
示例代码尝试做上述的一半,请注意,我已经写了redis,但我需要检索所有数据,以给予一致性,我的应用程序。这是必要的,因为我需要在Map上的速度。
let schedules = [];
personells.forEach((personell) => {
console.log("personell", personell.dataValues.mgr_wrkpersonnels_empid);
// Use Redis callback to retrieve the list
client.lRange(`user:${personell.dataValues.mgr_wrkpersonnels_empid}`, 0, -1, (err, reply) => {
schedules.push(reply);
console.log("reply", reply);
if (err) {
console.error('Redis Error:', err);
res.status(500).json({ error: 'Error retrieving keys from Redis' });
}
});
});
犯这个错误
GET /scheduler?mgr_wrkpersonnels_designation=2 304 17.748 ms - -
/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:490
return Promise.reject(new errors_1.ClientClosedError());
^
ClientClosedError: The client is closed
at Commander._RedisClient_sendCommand (/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:490:31)
at Commander.commandsExecutor (/home/hub/GBHL/node_modules/@redis/client/dist/lib/client/index.js:188:154)
at BaseClass.<computed> [as lRange] (/home/hub/GBHL/node_modules/@redis/client/dist/lib/commander.js:8:29)
at /home/hub/GBHL/workforceAPI/workforce-api/routes/scheduler.js:94:12
at Array.forEach (<anonymous>)
at /home/hub/GBHL/workforceAPI/workforce-api/routes/scheduler.js:91:14
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.16.1
[nodemon] app crashed - waiting for file changes before starting...
我试着用cli获取redis上的文件,我得到了所有的列表,没有问题。
这是一个lrange请求的示例
127.0.0.1:6379> lrange user:G1257 0 -1
1) "{\"id\":2,\"start\":\"2023-08-05T00:00:00\",\"end\":\"2023-08-12T00:00:00\"}"
2) "{\"id\":4,\"start\":\"2023-08-14T00:00:00\",\"end\":\"2023-08-19T00:00:00\"}"
127.0.0.1:6379>
emp ID采用格式Gundi,其中x是一个数字,因此我有几个用户的记录包含ID,开始,结束值。我想做的是提取一个给定用户的所有记录,通过他们的empid作为redis散列,采用user:empid的形式
1条答案
按热度按时间bksxznpy1#
我使用了multi exec,这是nodejs中的一个管道解决方案,可以让你使用redis Commander执行批处理命令。
下面是我的get请求
这是我的结果,非常顺利。我甚至有多个条目的散列。
忽略非唯一ID。这些只是对另一张table的引用。SHift ID,我用它来填充和呈现前端的一些信息。