云函数:TypeError:无法读取未定义

时间:2018-06-07 15:50:36

标签: node.js google-cloud-platform google-cloud-functions google-api-nodejs-client

我正在使用Google Cloud Functions并在控制台中编写了一个功能。但我一直收到这个错误:

TypeError:无法读取未定义的属性'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控制台界面中创建了此功能。 我试图在这里遵循这个例子,但我相信这是在本地建立的。而我正在尝试直接使用Google Cloud Functions控制台UI。因为我已经在GCP中并在控制台UI中编写此功能 - 我是否需要进行身份验证?它不应该拿起用户凭证和项目ID直接?

Cloud Functions: Detailed stack trace: Error: Cannot find module 'googleapis'

谢谢!

1 个答案:

答案 0 :(得分:1)

我刚做了一个快速测试,并且能够通过

使其工作
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"
  }
}