当我尝试从计划的云函数访问外部 API 时,我不断收到错误,错误:找不到模块"请求"



我通过firebase CLI创建了一个计划的云函数,每分钟运行一次,并从Elena sports football API端点收集数据,但我的云函数日志中不断出现错误,这是我的日志,我是新手,所以我选择了Node js(request(代码示例来测试APIs是否工作,我是否使用了错误的语言代码示例,我的印象是云函数可以使用node.js进行编码,请帮助阐明我做错了什么

11:15:00.247 PM
scheduledFunction
Hey mac , im working.....
11:15:00.249 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:15:00.249 PM
scheduledFunction
Error: Cannot find module 'request'
11:15:00.249 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:15:00.249 PM
scheduledFunction
Require stack:
11:15:00.249 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:15:00.249 PM
scheduledFunction
- /workspace/index.js
11:15:00.249 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:15:00.249 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:15:00.249 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:15:00.249 PM
scheduledFunction
at /workspace/index.js:16:19
11:15:00.249 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:15:00.249 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:15:00.249 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:15:00.250 PM
scheduledFunction
Function execution took 8 ms, finished with status: 'error'
11:16:00.242 PM
scheduledFunction
Function execution started
11:16:00.246 PM
scheduledFunction
Hey mac , im working.....
11:16:00.247 PM
scheduledFunction
Error: Cannot find module 'request'
11:16:00.247 PM
scheduledFunction
- /workspace/index.js
11:16:00.247 PM
scheduledFunction
Require stack:
11:16:00.247 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:16:00.247 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:16:00.247 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:16:00.247 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:16:00.247 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:16:00.247 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:16:00.247 PM
scheduledFunction
at /workspace/index.js:16:19
11:16:00.247 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:16:00.247 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:16:00.247 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:16:00.248 PM
scheduledFunction
Function execution took 7 ms, finished with status: 'error'
11:17:00.450 PM
scheduledFunction
Function execution started
11:17:00.460 PM
scheduledFunction
Hey mac , im working.....
11:17:00.467 PM
scheduledFunction
Error: Cannot find module 'request'
11:17:00.467 PM
scheduledFunction
- /workspace/index.js
11:17:00.467 PM
scheduledFunction
Require stack:
11:17:00.467 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:17:00.467 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:17:00.467 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:17:00.467 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:17:00.467 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:17:00.467 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:17:00.467 PM
scheduledFunction
at /workspace/index.js:16:19
11:17:00.467 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:17:00.467 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:17:00.467 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:17:00.468 PM
scheduledFunction
Function execution took 18 ms, finished with status: 'error'
11:18:00.449 PM
scheduledFunction
Function execution started
11:18:00.545 PM
scheduledFunction
Hey mac , im working.....
11:18:00.546 PM
scheduledFunction
Error: Cannot find module 'request'
11:18:00.546 PM
scheduledFunction
Require stack:
11:18:00.547 PM
scheduledFunction
- /workspace/index.js
11:18:00.547 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:18:00.547 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:18:00.547 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:18:00.547 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:18:00.547 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:18:00.547 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:18:00.547 PM
scheduledFunction
at /workspace/index.js:16:19
11:18:00.547 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:18:00.547 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:18:00.547 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:18:00.548 PM
scheduledFunction
Function execution took 100 ms, finished with status: 'error'

这是我的代码

const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//   functions.logger.info("Hello logs!", {structuredData: true});
//   response.send("Hello from Firebase!");
// });


exports.scheduledFunction = functions.pubsub.schedule('* * * * *').onRun((context) => {
console.log('Hey mac , im working.....');
const request = require('request');
const options = {
method: 'GET',
url: 'https://elenasport-io1.p.rapidapi.com/v2/upcoming',
qs: {page: '1'},
headers: {
'x-rapidapi-key': 'API KEY COMES HERE',
'x-rapidapi-host': 'elenasport-io1.p.rapidapi.com',
useQueryString: true
}
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});
});

您面临这个问题是因为请求包没有安装在firebase云功能中。只运行

npm install request --save

来自您的firebase云功能项目。它应该起作用。

相关内容

最新更新