我正在尝试在消费者中导入包含冗余代码的文件。到目前为止,似乎我的文件不被消费者阅读,它返回我:
函数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')