“Callback function is not a function” Error when following Google Cloud Scheduler / PubSub tutorial

别来无恙 提交于 2020-04-16 03:49:06

问题


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?

*Edit: 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;
}

回答1:


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

The source repo has been updated.




回答2:


Faced the same issue just an hour ago :)

Try taking callback as 3rd param:

exports.stopInstancePubSub = (event, data, callback) => { ... }

Hope it helps




回答3:


The problem is the runtime version.

Try to use: RuntimeNode.js 6 (Deprecated)

This solution solved for me



来源:https://stackoverflow.com/questions/56548965/callback-function-is-not-a-function-error-when-following-google-cloud-schedule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!