我试图从谷歌片的帮助下访问云数据库的FireStore FirestoreGoogleAppsScript例子,但在第5点说明我已经下载了文件,但没有得到哪里更新client_email
,private_key
并project_id
在项目中。所以请引导我前进。
当您按“创建”时,浏览器将下载.json
包含私钥(private_key
),服务帐户电子邮件(client_email
)和项目ID(project_id
)的文件。将这些值复制到您的Google Apps脚本中-您需要它们才能通过Firestore进行身份验证。
带有演示Code.gs
function myFunction() {
projectid: "xxx";
key: "-----BEGIN PRIVATE KEY-----\nPrivateKeyHere\n-----END PRIVATE KEY-----\n";
email: "[email protected]";
var firestore = FirestoreApp.getFirestore(email, key, projectId);
}
错误
ReferenceError: "email" is not defined. (line 7, file "Code")
您必须执行以下操作,例如,在一个用于获取集合的简单函数中:
function fecthFirstCollectionDocs() {
var email = "[email protected]";
var key = "-----BEGIN PRIVATE KEY-----\your key here\n-----END PRIVATE KEY-----\n";
var projectId = "zzzzzzzzz";
var firestore = FirestoreApp.getFirestore(email, key, projectId);
const allDocuments = firestore.query("FirstCollection").execute();
Logger.log(JSON.parse(allDocuments));
}
的值key
是通过创建服务帐户获得的,如库文档中所述:https : //github.com/grahamearley/FirestoreGoogleAppsScript#creating-a-service-account。这很容易,只需按照说明进行操作即可。
你必须只在部分之间复制-----BEGIN PRIVATE KEY-----
和-----END PRIVATE KEY-----\n
从.json
文件。
在您发表4月9日的评论后编辑:
解决
ReferenceError: "email" is not defined. (line 7, file "Code")
错误,应正确声明传递给该getFirestore()
方法的变量,如我的答案代码所示,如下所示:
而不是做
projectid: "fir-79c39";
你应该做
var projectid = "fir-79c39";
您声明一个projectid
在getFirestore()
方法中使用的名为的变量。同为key
和email
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句