我目前正在处理三个文件,它们是 index.js 、 index.main.js 和 app.js。我正在使用 request-context 从 index.main.js 中获取一个变量并将其传递给 index.js。
在 app.js (我在服务器文件夹中创建的文件)中,我有以下代码
//full code in app.js
const contextService = require("request-context");
const app = express();
app.use(contextService.middleware("request"));
我尝试运行以下这些命令
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
npm install -D @types/request-context
并尝试在导入前使用
// @ts-ignore
无济于事。
当我检查我的 app.js 时,我注意到单词“require”上的三个点显示:
找不到模块“请求上下文”的声明文件。'/home/servertest/Desktop/folder/folder1/src/component_NodeJS/server/node_modules/request-context/lib/index.js' 隐含一个 'any' 类型。尝试npm install @types/request-context
它是否存在或添加包含declare module 'request-context';
ts(7016)的新声明 (.d.ts) 文件
在 index.main.js 我有以下
async function listFilesInDepth()
{
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucketName = 'probizmy';
const [files] = await storage.bucket(bucketName).getFiles();
const contextService = require("request-context");
console.log('List Of Files Available:');
files.forEach(file =>
{
targetFiles = file.name;
console.log(`-----`+file.name);
});
contextService.set("request:targetFileKey", targetFiles);
return targetFiles;
}
在 index.js 我有以下代码
const contextService = require("request-context");
const targetFiles = contextService.get("request:targetFileKey");
console.log(targetFiles) //output shows undefined
我怀疑请求上下文错误是我未定义为输出的原因。我的预期结果是在控制台日志上输出 targetFiles 的值。
希望能对此有所了解。任何帮助将不胜感激!谢谢 :)
编辑:
根据要求,我已包含 package.json
{
"name": "server",
"version": "0.1.81",
"description": "Server NodeJS For Internel Process",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@google-cloud/storage": "^2.4.2",
"@google-cloud/vision": "^0.25.0",
"@types/jest": "^24.0.15",
"@types/node": "^12.0.12",
"@types/react": "^16.8.23",
"@types/react-dom": "^16.8.4",
"alphabet-generator": "^1.0.1",
"body-parser": "^1.18.3",
"cheerio": "^1.0.0-rc.2",
"cors": "^2.8.5",
"express": "^4.16.4",
"format": "^0.2.2",
"grpc": "^1.19.0",
"multer": "^1.4.1",
"natural": "^0.6.3",
"path": "^0.12.7",
"request": "^2.88.0",
"request-context": "^2.0.0",
"require-all": "^3.0.0",
"require-dir": "^1.2.0",
"string-similarity": "^3.0.0",
"typescript": "^3.5.2"
},
"devDependencies": {
"babel-plugin-root-import": "^6.2.0"
}
}