首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我试图从计划好的云函数访问外部API时,我一直收到一个错误:无法找到模块‘请求’

当我试图从计划好的云函数访问外部API时,我一直收到一个错误:无法找到模块‘请求’
EN

Stack Overflow用户
提问于 2021-04-10 21:23:43
回答 1查看 225关注 0票数 0

我通过firebase创建了一个计划好的云函数,以便每分钟运行一次,并从埃琳娜体育APIs端点收集数据,但是我不断地在我的云函数日志中发现错误,这里是我的日志,我是新手,所以我选择了node.js (请求)代码示例来测试APIS是否有效,我是否使用了错误的语言代码示例,我认为云函数可以使用node.js编码,请帮助了解我做错了什么。

代码语言:javascript
复制
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'

这里是我的密码

代码语言:javascript
复制
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);
  });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-10 21:46:24

您正面临这个问题,因为请求包没有安装在防火墙云功能中。快跑

代码语言:javascript
复制
npm install request --save

从您的防火墙云功能项目。应该管用的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67039391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档