【发布时间】:2021-01-01 05:52:26
【问题描述】:
我正在 Google Cloud Function 中尝试此代码,由节点 10 的 Cloud Storage 触发。
我不确定如何使上下文可用。 Event: ${context.eventId}
我试过了:module.exports.getDevices(context) = getDevices;
我试过了:module.exports.getDevices = getDevices(context);
async function getDevices() {
console.log(` Event: ${context.eventId}`);
const cloudRegion = '....';
const projectId = '.....';
const registryId = '.....';
const iot = require('@google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({});
const parentName = iotClient.registryPath(projectId, cloudRegion, registryId);
try {
const responses = await iotClient.listDevices({parent: parentName});
const devices = responses[0];
if (devices.length > 0) {
console.log('Current devices in registry:');
} else {
console.log('No devices in registry.');
}
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
console.log(`Device ${i}: `, device);
}
} catch (err) {
console.error('Could not list devices', err);
}
}
module.exports.getDevices = getDevices;
另外,这可行,但我想要和前一个一样的异步功能:
exports.helloGCS = (file, context) => {
console.log(` Event: ${context.eventId}`);
};
【问题讨论】:
标签: javascript node.js google-cloud-platform google-cloud-functions