"Callback function is not a function" 遵循 Google Cloud Scheduler / PubSub 教程时出错
"Callback function is not a function" Error when following Google Cloud Scheduler / PubSub tutorial
我正在尝试为 Google 云上的 VM 实例创建 start/stop 计划。我正在关注由 Google 创建的 tutorial 但是当我到达 (Optional) Verify the functions work 部分并尝试测试 stopInstancePubSub 函数并传递 {"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="} JSON 对象我得到以下错误:
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)
不确定我在这里做错了什么,我是否缺少传递给函数的另一个参数?
*编辑:正在使用的代码取自 Googles 教程:
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;
}
一个小时前遇到了同样的问题:)
尝试将 callback
作为第三个参数:
exports.stopInstancePubSub = (event, data, callback) => { ... }
希望对您有所帮助
尝试exports.stopInstancePubSub = (event, context, callback) => { ... }
运行时版本问题
尝试使用:RuntimeNode.js6(已弃用)
这个解决方案帮我解决了
我正在尝试为 Google 云上的 VM 实例创建 start/stop 计划。我正在关注由 Google 创建的 tutorial 但是当我到达 (Optional) Verify the functions work 部分并尝试测试 stopInstancePubSub 函数并传递 {"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="} JSON 对象我得到以下错误:
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)
不确定我在这里做错了什么,我是否缺少传递给函数的另一个参数?
*编辑:正在使用的代码取自 Googles 教程:
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;
}
一个小时前遇到了同样的问题:)
尝试将 callback
作为第三个参数:
exports.stopInstancePubSub = (event, data, callback) => { ... }
希望对您有所帮助
尝试exports.stopInstancePubSub = (event, context, callback) => { ... }
运行时版本问题
尝试使用:RuntimeNode.js6(已弃用)
这个解决方案帮我解决了