Firestore使用Cloud Function删除文档并发送通知

时间:2018-10-29 06:39:34

标签: android node.js firebase google-cloud-firestore google-cloud-functions

我的云功能中的node.js脚本遇到一些问题。我对JavaScript和使用node.js相当陌生。我的云函数用于我的android应用程序,我在执行https.onCall并附加了数据,我希望脚本使用所使用的数据(是UID)从Firestore数据库中删除文档,并在文档中添加字段在删除文档之前,我拥有要获取的用户UID的数组列表,因此我可以向每个用户发送通知,告知其删除。这是我的代码,在我对其进行测试时会在日志中触发,但无法完全正常工作。

exports.deleteSession = functions.https.onCall((data) => {
	const DB = admin.firestore();
	console.log("function Triggered");
	const UID = data.text;
	console.log(`got the text ${UID}`);
	const sessionOb = DB.collection('Sessions').doc(`${UID}`).get();
	console.log(`got session object ${sessionOb.data().tutorUID}`);

	var students = [];
	var TUID = sessionOb.data().tutorUID;
	students = sessionOb.data().students;

	var array_uid = [];

	for(var key in students){
		array_uid.push(key);
	}

	var tokens = [];

	for(var id in array_uid){
		var userOB = DB.collection('Users').doc(`${id}`).get();
		tokens.push(userOB.data().tokenID);
	}

	console.log("functions in middle");

	var tutorOB = DB.collection('Users').doc(`${TUID}`).get();

	var tutorName = tutorOB.data().name;
	var profile = tutorOB.data().profileImage;

	const payload = {
			notification: {
				title: `${tutorName} has canceled their Session`,
				body: `${session.subject} tutoring is Canceled`,
				icon: profile
			}
		};

	var deleteDoc = DB.collection('Sessions').doc(`${UID}`).delete();
	console.log("document deleted");

	return admin.messaging().sendToDevice(tokens, payload).then(function(response) {
								console.log("Successfully sent message:", response);
							  })
							  .catch(function(error) {
								console.log("Error sending message:", error);
							  });

});

0 个答案:

没有答案