【问题标题】:How to extract data using async and await function in node js?如何在节点 js 中使用 async 和 await 函数提取数据?
【发布时间】:2019-05-01 11:11:07
【问题描述】:

我尝试使用mongodb在node js中实现redis缓存。我将数据设置在缓存中。但是我无法获取缓存中的数据。如何解决这个问题。

cache.js

async function Get_Value(){
    let response = await client.get('products')
    console.log("_______________")
    console.log(response)

}

我得到了输出:真

异常输出:json 数据

如何使用缓存get方法获取json数据

【问题讨论】:

    标签: node.js node-redis nodejs-server


    【解决方案1】:

    Redis 没有为 node.js 提供完整的异步等待适配器,因此通常作为一种解决方法,人们会承诺该库。

    const { promisify } = require('util');
    const getAsync = promisify(client.get).bind(client);
    
    async function getValue(){
        let response = await getAsync("products");
    }
    

    另一种方法来承诺您可以使用的整个 redis 库:

    const redis = require('redis');
    bluebird.promisifyAll(redis);
    

    现在您还可以使用async/await 的方法。

    【讨论】:

    • 我无法理解如何使用 bluebird.promisifyAll(redis);
    • 为了不单独承诺每种方法,您一次承诺所有方法。
    猜你喜欢
    • 2020-02-14
    • 2021-09-27
    • 1970-01-01
    • 2020-08-01
    • 2017-06-05
    • 2022-01-27
    • 1970-01-01
    • 2022-01-22
    • 2020-11-07
    相关资源
    最近更新 更多