Firebase云function与云Firestore的麻烦

我在以前的项目中使用过这个Firebase数据库代码:

const getDeviceUser = admin.database().ref(`/users/${notification.to}/`).once('value'); 

我现在正试图将其转换为Firestore。 我基本上是试图让我的用户fcm当通知正在发送。 我已经尝试了很多东西,但还没有看到实现这一目标的新方法。

编辑:这是我的代码。

 exports.sendFavoriteNotification = functions.firestore.document('users/{userUid}/notifications/{notificationId}').onCreate(event => { const notification = event.data.data(); const user = event.params.userUid; const getDeviceUser = admin.database().ref(`/users/${notification.to}/`).once('value'); // Get the follower profile. const getProfilePromise = admin.auth().getUser(notification.sender); return Promise.all([getDeviceUser, getProfilePromise]).then(results => { const tokensSnapshot = results[0]; const liker = results[1]; // Check if there are any device tokens. if (!tokensSnapshot.hasChildren()) { return console.log('There are no notification tokens to send to.'); } //console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.'); console.log('Fetched follower profile', liker); // Notification details. const payload = { notification : { title : 'You have a new like!', body : `${liker.displayName} just liked your photo.`, badge: '1', sound: 'default' } }; // Listing all tokens. var tokens = admin.firestore.ref(`/users/${notification.to}/`).get('fcm'); // Send notifications to all tokens. admin.messaging().sendToDevice(tokens.data(), payload); return admin.messaging().sendToDevice(tokens, payload).then(response => { // For each message check if there was an error. const tokensToRemove = []; response.results.forEach((result, index) => { const error = result.error; if (error) { console.error('Failure sending notification to', tokens[index], error); // Cleanup the tokens who are not registered anymore. if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') { tokensToRemove.push(tokensSnapshot.update({ fcm: FieldValue.delete() })); } } }); return Promise.all(tokensToRemove); }); }); 

});

希望这会有所帮助。 这是我的代码2天后,试图学习如何从实时数据库转换为firestore。 它基于一个Firebase项目: https : //github.com/MahmoudAlyuDeen/FirebaseIM

 let functions = require('firebase-functions'); let admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotificationToFirestone = functions.firestore.document('/notifications/{pushId}') .onCreate(event => { const pushId = event.data.id; const message = event.data.data(); const senderUid = message.from; const receiverUid = message.to; const db = admin.firestore(); if (senderUid === receiverUid) { console.log('pushId: '+ pushId); return db.collection('notifications').doc(pushId).delete();; } else { const ref = db.collection('users').doc(receiverUid); const query = new Promise( function (resolve, reject) { ref.get() .then(doc => { if (!doc.exists) { console.log('No such document!'); reject(new Error('No such document!')); } else { console.log('Document data:', doc.data().instanceId); resolve(doc.data().instanceId); } }) .catch(err => { console.log('Error getting document', err); reject(err); }); }); const getSenderUidPromise = admin.auth().getUser(senderUid); return Promise.all([query, getSenderUidPromise]).then(results => { //console.log('instanceId = Result[0]: ' + results[0]); //console.log('sender = Result[1]: ' + results[1]); const instanceId = results[0]; const sender = results[1]; //console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid); //console.log('instanceId este' + instanceId); const payload = { notification: { title: sender.displayName, body: message.body, icon: sender.photoURL } }; admin.messaging().sendToDevice(instanceId, payload) .then(function (response) { console.log("Message sent: ", response); }) .catch(function (error) { console.log("Error sending message: ", error); }); }); } });