“回调函数不是函数";遵循Google Cloud Scheduler/PubSub教程时出错 [英] "Callback function is not a function" Error when following Google Cloud Scheduler / PubSub tutorial

查看:129
本文介绍了“回调函数不是函数";遵循Google Cloud Scheduler/PubSub教程时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我在Google Cloud上的VM实例创建一个开始/停止时间表.我正在按照此教程由Google创建,但是当我进入(可选)验证函数工作部分并尝试测试 stopInstancePubSub 函数并传递 {数据时:" eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo =} JSON对象出现以下错误:

I am trying to create to a start/stop schedule for my VM instance on Google Cloud. I am following this tutorialcreated by Google but when I get to the (Optional) Verify the functions work section and try to test the stopInstancePubSub function and pass the {"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="} JSON object I get the following error:

2019-06-09 17:23:54.225 EDT
stopInstancePubSub
ipmdukx38xpw
TypeError: callback is not a function at exports.stopInstancePubSub (/srv/index.js:55:5) at /worker/worker.js:825:24 at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

不知道我在这里做错了什么,我是否错过了另一个要传递给函数的参数?

Not sure what I am doing wrong here, am I missing another argument to pass to the function?

*正在使用的代码摘自Google教程:

* The code being used is taken from Googles Tutorial:

const Buffer = require('safe-buffer').Buffer;
const Compute = require('@google-cloud/compute');
const compute = new Compute();

/**
 * Stops a Compute Engine instance.
 *
 * Expects a PubSub message with JSON-formatted event data containing the
 * following attributes:
 *  zone - the GCP zone the instances are located in.
 *  instance - the name of a single instance.
 *  label - the label of instances to start.
 *
 * Exactly one of instance or label must be specified.
 *
 * @param {!object} event Cloud Function PubSub message event.
 * @param {!object} callback Cloud Function PubSub callback indicating completion.
 */
exports.stopInstancePubSub = (event, callback) => {
  try {
    const pubsubMessage = event.data;
    const payload = _validatePayload(
      JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString())
    );
    const options = {filter: `labels.${payload.label}`};
    compute.getVMs(options).then(vms => {
      vms[0].forEach(instance => {
        if (payload.zone === instance.zone.id) {
          compute
            .zone(payload.zone)
            .vm(instance.name)
            .stop()
            .then(data => {
              // Operation pending.
              const operation = data[0];
              return operation.promise();
            })
            .then(() => {
              // Operation complete. Instance successfully stopped.
              const message = 'Successfully stopped instance ' + instance.name;
              console.log(message);
              callback(null, message);
            })
            .catch(err => {
              console.log(err);
              callback(err);
            });
        }
      });
    });
  } catch (err) {
    console.log(err);
    callback(err);
  }
};

/**
 * Validates that a request payload contains the expected fields.
 *
 * @param {!object} payload the request payload to validate.
 * @return {!object} the payload object.
 */
function _validatePayload(payload) {
  if (!payload.zone) {
    throw new Error(`Attribute 'zone' missing from payload`);
  } else if (!payload.label) {
    throw new Error(`Attribute 'label' missing from payload`);
  }
  return payload;
}

推荐答案

尝试exports.stopInstancePubSub = (event, context, callback) => { ... }

源存储库已更新.

这篇关于“回调函数不是函数";遵循Google Cloud Scheduler/PubSub教程时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆