Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 测试hset和hget时使用ioredis或node_redis的奇怪行为_Node.js_Redis_Mocha.js_Node Redis_Ioredis - Fatal编程技术网

Node.js 测试hset和hget时使用ioredis或node_redis的奇怪行为

Node.js 测试hset和hget时使用ioredis或node_redis的奇怪行为,node.js,redis,mocha.js,node-redis,ioredis,Node.js,Redis,Mocha.js,Node Redis,Ioredis,希望有人能解释一下。我一辈子都找不到问题所在 问题: 我试图使用hset在一个文件中为Redis添加一个值,并使用hget在另一个文件中获取该值。在测试用例中最终发生的情况是设置了值,但hget返回null,就好像什么都没有一样 尝试: 当我在相同的文件中设置值后立即尝试获取该值时,会出现非常奇怪的情况。它实际上正常工作!看看这个 // FYI: This is co-mocha so the generator function is okay describe('Route', () =&g

希望有人能解释一下。我一辈子都找不到问题所在

问题:

我试图使用hset在一个文件中为Redis添加一个值,并使用hget在另一个文件中获取该值。在测试用例中最终发生的情况是设置了值,但hget返回null,就好像什么都没有一样

尝试:

当我在相同的文件中设置值后立即尝试获取该值时,会出现非常奇怪的情况。它实际上正常工作!看看这个

// FYI: This is co-mocha so the generator function is okay
describe('Route', () => {
  it('should return the value currently set in Redis', function* () {
    const link = 'this-is-a-link';
    yield redis.hset('i:metadata', 'stream', link); // using ioredis
    const response = yield request.send(); // requests to my Koa server
    expect(response.body.stream).to.equal(link); // expected null to equal 'this-is-a-link'
  });
});
你可以看到路线很简单

// FYI: This is a Koa route reference with a HTTP GET on '/'
exports.get = function* () {
  const stream = yield redis.hget('i:metadata', 'stream'); // using ioredis
  this.body = { stream }; // holds a value of null
};
如果我在我的测试用例中,在我的hset的正下方插入该hget,则可以完美地检索该值。有人对如何解决这个问题有什么想法吗

注意:

在任何人建议之前,我已经听过相关事件,这不重要,因为我可以在hset下立即设置值,并且它可以工作。此问题在promisified node_redis安装中也会持续存在

谢谢