我正在使用 Google Cloud Functions 并在控制台中编写了一个函数。但我不断收到此错误:
类型错误:无法读取未定义的属性“auth”
这是我的index.js
var {google} = require('googleapis');
const { auth } = require('google-auth-library');
exports.goWithTheDataFlow = (event, callback) => {
const file = event.data;
const context = event.context;
console.log(`Event ${context.eventId}`);
console.log(` Event Type: ${context.eventType}`);
console.log(` Bucket: ${file.bucket}`);
console.log(` File: ${file.name}`);
console.log(` Metageneration: ${file.metageneration}`);
console.log(` Created: ${file.timeCreated}`);
console.log(` Updated: ${file.updated}`);
google.auth.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
throw err;
}
console.log(projectId);
const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
console.log(`gs://${file.bucket}/${file.name}`);
dataflow.projects.templates.create({
projectId: projectId,
resource: {
parameters: {
inputFile: `gs://${file.bucket}/${file.name}`
},
jobName: 'cloud-fn-beam-test',
gcsPath: 'gs://goldsgymdemo/templates/MyGCStoBQDFTemplate'
}
}, function(err, response) {
if (err) {
console.error("problem running dataflow template, error was: ", err);
}
console.log("Dataflow template response: ", response);
callback();
});
});
callback();
};
这是我的 package.json。
{
"name": "sample-cloud-storage",
"version": "0.0.1",
"dependencies": {
"googleapis": "^21.3.0",
"google-auth-library": "^1.6.0"
}
}
我直接在 Google Cloud Functions Console UI 中创建了此函数。 我正在尝试遵循此处的示例,但我相信这是在本地构建的。而我尝试直接使用 Google Cloud Functions Console UI。因为我已经在 GCP 中并在控制台 UI 中编写了此函数 - 我需要进行身份验证吗?难道它不应该直接获取用户凭据和项目 ID 吗?
Cloud Functions: Detailed stack trace: Error: Cannot find module 'googleapis'
谢谢!
最佳答案
我刚刚做了一个快速测试,并能够通过使用使其工作
const google = require('googleapis');
并删除
const { auth } = require('google-auth-library');
另外,我使用了以下 package.json
{
"name": "sample-cloud-storage",
"version": "0.0.1",
"dependencies": {
"googleapis": "24.0.0"
}
}
关于node.js - 云函数: TypeError: Cannot read property 'auth' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745471/