Google Cloud Function / google-auth-library:无法读取未定义的属性“ user”

时间:2019-09-06 16:32:04

标签: javascript google-cloud-functions gcloud google-auth-library

此后:https://medium.com/@nedavniat/how-to-perform-and-schedule-firestore-backups-with-google-cloud-platform-and-nodejs-be44bbcd64ae

代码是:

const functions = require('firebase-functions'); // is installed automatically when you init the project
const { auth } = require('google-auth-library'); // is used to authenticate your request

async function exportDB () {
    const admin = await auth.getClient({
        scopes: [                               // scopes required to make a request
            'https://www.googleapis.com/auth/datastore',
            'https://www.googleapis.com/auth/cloud-platform'
        ]
    });
    const projectId = await auth.getProjectId();
    const url = `https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments`;
    return admin.request({
        url,
        method: 'post',
        data: {
            outputUriPrefix: 'gs://name-of-the-bucket-you-created-for-backups'
        }
    });
}

const backup = functions.pubsub.topic('YOUR_TOPIC_NAME_HERE')。onPublish(exportDB); module.exports = {备份};

当我通过以下方式进行部署时:

gcloud functions deploy backup --runtime nodejs8 --trigger-topic YOUR_TOPIC_NAME_HERE

我收到错误消息:

  

错误:(gcloud.functions.deploy)OperationError:代码= 3,   message =功能在加载用户代码时失败。错误消息:输入代码   无法加载文件index.js。您的代码中是否存在语法错误?

     

详细的堆栈跟踪:TypeError:无法读取属性'user'的   未定义

这与google-auth-library有关吗?

1 个答案:

答案 0 :(得分:0)

我假设您正在尝试部署由HTTP请求触发的GCF功能,建议您检查此链接[1]似乎是相同的用例,并且可以帮助您在GCF上将Node.js与Google Cloud Datastore结合使用< / p>

[1] How to return an entire Datastore table by name using Node.js on a Google Cloud Function