Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript google云任务未向http云函数发送正文_Javascript_Node.js_Google Cloud Platform_Google Cloud Functions_Google Cloud Tasks - Fatal编程技术网

Javascript google云任务未向http云函数发送正文

Javascript google云任务未向http云函数发送正文,javascript,node.js,google-cloud-platform,google-cloud-functions,google-cloud-tasks,Javascript,Node.js,Google Cloud Platform,Google Cloud Functions,Google Cloud Tasks,我有一个gcloud任务,其中的代码大部分是从cloudtasks文档中复制的。 cloud函数的目标是能够将dateTo和dateFrom日期推送到它,这样它就可以循环周期并从中创建cloudTasks。我还没有创建循环,因为我首先想解决这个问题 问题是它没有将主体推送到http云函数。 http云函数在使用CURL时工作 curl-X POST“postrl”-H“内容类型:application/json”--数据“{”date:“2019-12-01”,“lastRun”:false}

我有一个gcloud任务,其中的代码大部分是从cloudtasks文档中复制的。

cloud函数的目标是能够将dateTo和dateFrom日期推送到它,这样它就可以循环周期并从中创建cloudTasks。我还没有创建循环,因为我首先想解决这个问题

问题是它没有将主体推送到http云函数。 http云函数在使用CURL时工作

curl-X POST“postrl”-H“内容类型:application/json”--数据“{”date:“2019-12-01”,“lastRun”:false}”

我检查了这里提到的方法,它是POST,所以应该可以

检查接口是否没有有效负载。使用gcloud beta描述任务。。。没有尸体,也没有任何有效载荷

httpRequest:
  headers:
    User-Agent: Google-Cloud-Tasks
  httpMethod: POST
  url: correcthttpurl
name: name
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: view
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: BASIC
这是创建任务的云函数的代码。 任务被添加到队列中,但单击“运行”时,它们似乎不会触发函数。(可能是因为该功能需要一个主体才能工作)

我错过了什么

如您所见,任务已添加但未执行

根据谷歌的说法,负载和标题是空的,默认情况下,responseView是基本的;并非所有信息都是默认检索的,因为一些数据(如有效载荷)可能只在需要时才返回,因为它的大小很大,或者因为它包含的数据很敏感。

根据谷歌的说法,默认情况下,responseView是基本的;默认情况下不会检索所有信息,因为某些数据(如有效负载)可能需要仅在需要时返回,因为其大小较大,或者因为其中包含的数据的敏感性。

我发现了问题,由于某些原因,应用程序引擎未启用。请求正在工作

我发现了问题,应用程序引擎由于某种原因未启用。请求正在工作

我在后面的代码中将其更改为“FULL”,数据在响应中。但这并不能解决我的问题。任务似乎没有执行。@Christoph,任务是在云任务中创建的吗?是的,它在列表中。但是,如果单击任务名称,则没有有效负载。查看我的云函数日志,它似乎没有被触发。单击“执行”按钮时,它会运行,但在云函数日志中我看不到任何内容。1。您的云功能是否被HTTP触发?2.云任务中的任务是否已删除?我在稍后的代码中将其更改为“完整”,并且数据在响应中。但这并不能解决我的问题。任务似乎没有执行。@Christoph,任务是在云任务中创建的吗?是的,它在列表中。但是,如果单击任务名称,则没有有效负载。查看我的云函数日志,它似乎没有被触发。单击“执行”按钮时,它会运行,但在云函数日志中我看不到任何内容。1。您的云功能是否被HTTP触发?2.云任务中的任务是否已删除?是否已尝试进行测试?您可以使用此处所示的作用域和请求URL:@ziganotschka inserting works,执行似乎不起作用。我正在运行strackdriver日志记录,那里也没有日志。您尝试过测试吗?您可以使用此处所示的作用域和请求URL:@ziganotschka inserting works,执行似乎不起作用我正在运行strackdriver日志,那里也没有日志。很好,您找到了根本原因。很好,您找到了根本原因。
/**
 * Background Cloud Function to be triggered by Pub/Sub.
 * This function is exported by index.js, and executed when
 * the trigger topic receives a message.
 *
 * @param {object} pubSubEvent The event payload.
 * @param {object} context The event metadata.
 */

// gcloud functions deploy queueAffiliateApiTasks --trigger-topic queue-affiliate-api-tasks --region europe-west1 --runtime=nodejs8

const moment = require("moment");


exports.queueAffiliateApiTasks = async (pubSubEvent, context) => {
  const data =
    pubSubEvent.data || Buffer.from(pubSubEvent.data, "base64").toString();

  const attributes = pubSubEvent.attributes;

  // take 30 days ago untill yesterday
  let dateFrom = moment().subtract(1, "days");
  let dateTo = moment().subtract(1, "days");

  // if dates provided in pubsub use those
  if (attributes && "dateFrom" in attributes && "dateTo" in attributes) {
    console.log("with attributes");
    dateFrom = attributes.dateFrom;
    dateTo = attributes.dateTo;
  } else {
    console.log("no attributes");
  }

  console.log(dateFrom);
  console.log(dateTo);

  // use dates for looping
  dateFrom = moment(dateFrom);
  dateTo = moment(dateTo);

  console.log(dateFrom);
  console.log(dateTo);

  const date = dateTo.format("YYYY-MM-DD").toString();
  const lastRun = false;
  const url =
    "the correct url to the http cloud function";
  const payload = JSON.stringify({ date: date, lastRun: false }, null, 2);

  await createHttpTask(url, payload);
};

async function createHttpTask(url, payload) {
  const project = "xxx";
  const queue = "affiliate-api-queue";
  const location = "europe-west1";
  const inSeconds = 0 // Delay in task execution
  // [START cloud_tasks_create_http_task]
  // Imports the Google Cloud Tasks library.
  const {CloudTasksClient} = require('@google-cloud/tasks');

  // Instantiates a client.
  const client = new CloudTasksClient();

  // Construct the fully qualified queue name.
  const parent = client.queuePath(project, location, queue);

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url,
    },
  };

  task.httpRequest.body = Buffer.from(payload).toString('base64');

  if (inSeconds) {
    // The time when the task is scheduled to be attempted.
    task.scheduleTime = {
      seconds: inSeconds + Date.now() / 1000,
    };
  }

  // Send create task request.
  console.log('Sending task:');
  console.log(task);
  const request = {parent, task};
  const [response] = await client.createTask(request);
  console.log(`Created task ${response.name}`);
  console.log(`Response: ${JSON.stringify(response.httpRequest, null, 2)}`);
  // [END cloud_tasks_create_http_task]
}