Redis - 我正在尝试在我的 NodeJS 中导入文件而不将其分配给变量,到目前为止它不起作用



简介

我正在尝试导入一个包含消费者中冗余代码的文件。到目前为止,看来我的文件还没有由我返回我的消费者阅读:

函数getAsync未定义

但是它在file1.js

中定义

在这里我的片段:

file1.js:

const redis=require("redis"); 
rejson = require('redis-rejson');
const {promisify} = require('util'); 
rejson(redis); /* important - this must come BEFORE creating the client */
let client= redis.createClient({
    port:6380,
    host:'localhost',
    // password:process.env.rPass
});  
const setAsync = promisify(client.json_set).bind(client);
const arrappendAsync = promisify(client.json_arrappend).bind(client);
const getAsync = promisify(client.json_get).bind(client); 
const existsAsync= promisify(client.exists).bind(client);

file2.js:

require("./redisConnect")
async function getUser(){ 
    let res=await getAsync("userStock", ".")
        .catch((err) => console.error(err)); 
    resArray= JSON.parse(res)
    console.log("userStock res: ", resArray[0]);
    client.quit()
}
// use userId to filter relevant array 
getUser()

那么如何处理这种情况?

任何提示都很棒,谢谢

在file1.js中,您需要做:

module.exports = getAsync;

然后在file2.js中,执行此操作:

var getAsync = require('./file1.js')

最新更新