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

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

Brief

我正在尝试在消费者中导入包含冗余代码的文件。到目前为止,似乎我的文件不被消费者阅读,它返回我:

函数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()

那怎么处理这种情况呢?

任何提示都会很棒,谢谢

javascript node.js import require node-redis
1个回答
0
投票

在file1.js中,您需要执行以下操作:

module.exports = getAsync;

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

var getAsync = require('./file1.js')
© www.soinside.com 2019 - 2024. All rights reserved.