如何使用节点js中的async和await函数提取数据?

问题描述 投票:1回答:1

我使用mongodb在节点js中尝试了redis缓存工具。我在缓存中设置数据。但我无法获取cache.how中的数据来解决此问题。

cache.js

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

}

我得到了输出:true

例外输出:json数据

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

node.js node-redis nodejs-server
1个回答
1
投票

Redis没有为node.js提供完整的异步等待适配器,因此通常作为一种解决方法,人们正在宣传lib。

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的方法。

© www.soinside.com 2019 - 2024. All rights reserved.