当我尝试从预定的云函数访问外部 API 时,我不断收到错误消息:错误:找不到模块“请求”

麦克

我通过我的 firebase CLI 创建了一个预定的云函数,每分钟运行一次,并从 Elena 体育足球 API 端点收集数据,但我的云函数日志中不断出现错误,这是我的日志,我是新手,所以我选择了 Node js (请求)代码示例来测试 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 云功能项目。它应该工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章