我正在尝试在模拟器中测试firebase云函数。我已经能够在模拟器中CRUD firestore数据。我可以在模拟器中调用云函数。但我删除集合的函数是访问google云,而不是在模拟器中执行操作。
要删除集合,谷歌建议您使用"firebase_tools“来执行操作(https://firebase.google.com/docs/firestore/solutions/delete-collections)。
下面是我的云函数在模拟器中运行的简化版本:
//Delete a firestore document.
//When run in the emulator, this will successfully delete the piece of data in our emulator.
const paPath = FirebasePathUtils.getTeamPropertyAnalysisPath(pa.parentTeamId, pa.parentWorkspaceId, pa.id);
const p1 = admin.firestore().doc(paPath).delete();
//Delete all data in a collection.
//When run in the emulator this IS REACHING OUT TO THE REAL CLOUD NOT USING THE EMULATOR.
const paDataPath = FirebasePathUtils.getTeamPropertyAnalysisDataPath(teamId, wsId, paId);
const pa2 = firebase_tools.firestore
.delete(paDataPath, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then( () => {
console.log(`Successfully Deleted data under ${paDataPath}`);
});
return Promise.all([p1, p2]);当我在模拟器中运行云函数,并使用"firebase_toosl“执行删除集合代码时,我得到了输出
Google API requested!
- URL: "https://firestore.googleapis.com/v1beta1/projects/reitools-test/databases/(default)/documents/teamdata/-LjcN--jRrirOzDD0e3I/workspace_data/-LjcN-vktqOC8BJR_8X1/property_analyses_data/-LgJZf7GLRetC2fsVRvb"
- Be careful, this may be a production service.
⚠ Google API requested!
- URL: "https://firestore.googleapis.com/v1beta1/projects/reitools-test/databases/(default)/documents/teamdata/-LjcN--jRrirOzDD0e3I/workspace_data/-LjcN-vktqOC8BJR_8X1/property_analyses_data/-LgJZf7GLRetC2fsVRvb:runQuery"
- Be careful, this may be a production service.关于如何让"firebase_tools“对仿真器执行它的操作,有什么想法吗?
发布于 2020-05-12 19:07:59
我发现你已经有8个月没有发布你的问题了。我希望你已经解决了你的问题。
如果没有,那么问题出在您的环境变量"process.env.GCLOUD_PROJECT“上。
我相信在您的测试中,您正在初始化一个模拟的firestore实例,如下所示:
firebase.initializeAdminApp({ projectId: 'deletetest' }).firestore()在这种情况下,您应该使用'deletetest‘覆盖GCLOUD_PROJECT变量,该变量是您模拟的项目实例的projectId。
它很简单,就像
process.env.PROJECT_ID = 'deleteclub'我认为使用Jest时,变量只会被覆盖到测试文件中,所以如果其他测试需要这个环境变量,它不会引起任何问题,当然,您需要在每个测试需要的地方覆盖它。
我希望这能让人高兴:)
干杯!
https://stackoverflow.com/questions/57641067
复制相似问题