我已经构建了一个简单的云函数来访问Firestore集合上的文档,但是当尝试访问端点时,我遇到了ReferenceError
我的项目的GCP资源位置为“ southamerica-east1” “ test_collection”集合是从firebase界面创建的,添加了以下格式的两个文档:
index.js文件中的云函数代码:
exports.getCollectionData = functions
.https.onRequest((req, res) => {
admin.firestore().collection('test_collection').get()
.then(data => {
let test_collection = [];
data.forEach(doc => {
test_collection.push(doc.data());
});
return res.json(test_collection);
})
.catch(err => console.error(err));
})
但出现此错误:
ReferenceError: test_collection is not defined at data.forEach.doc (/srv/index.js:17:17) at QuerySnapshot.forEach (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:724:22) at admin.firestore.collection.get.then.data (/srv/index.js:16:18) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)
由于该函数的端点位于us-central1中,所以我认为这是Firestore的连接问题,因此我尝试构建一个指定“ southamerica-east1”区域的新函数
exports.getCollectionDataSAe1 = functions
.region('southamerica-east1')
.https.onRequest((req, res) => {
admin.firestore().collection('test_collection').get()
.then(data => {
let test_collection = [];
data.forEach(doc => {
test_collection.push(doc.data());
});
return res.json(test_collection);
})
.catch(err => console.error(err));
})
但是firebase不会对其进行解析,表明这不是受支持的区域
Error: Error occurred while parsing your function triggers. Error: The only valid regions are: us-central1, us-east1, us-east4, europe-west1, europe-west2, asia-east2, asia-northeast1
原始代码中有什么问题吗? 要使用此云功能成功访问收藏集,我有哪些选择?
thx