I've encountered the following code for cloud functions, which is intended to send a notification to the user upon the creation of a new follower. However, I'm facing an issue regarding converting the snap into a string in order to address the error message provided below.
function sendNewFollowNotificationFunc(uuid: string, newFollower: string) {
// const userRef = admin.database().ref('PeopleWhoFollowMe').child(uuid)
// const fetchAndUpdate = userRef.once('value')
console.log('User to send notification', uuid);
admin.database().ref(`UsersFireTokens`).child(uuid).child("token").once('value')
.then(snap => {
const token = snap.val//I dont know how, from here I should be handling this inorder to get the value at this location which is the token
// This registration token comes from the client FCM SDKs.
var registrationToken = token;
var message = {
data: {
text: 'This is a test',
time: '2:45'
},
token: registrationToken
};
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)//error here
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
})
.catch(error => {
console.log("Error in catch: "+error)
response.status(500).send(error)
})
Several errors are occurring, such as:
Argument of type '{ data: { text: string; time: string; }; token: string | null; }' is not assignable to parameter of type 'Message'. Type '{ data: { text: string; time: string; }; token: string | null; }' is not assignable to type 'TokenMessage'. Types of property 'token' are incompatible. Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.ts(2345)
What am I doing wrong and how can I fix it?
Update:
This is my code:
function sendNewFollowNotificationFunc(uuid: string, newFollower: string) {
// const userRef = admin.database().ref('PeopleWhoFollowMe').child(uuid)
// const fetchAndUpdate = userRef.once('value')
console.log('User to send notification', uuid);
admin.database().ref(`UsersFireTokens`).child(uuid).child("token").once('value')
.then(snap => {
//What should I be doing here and how?
console.log('token value', snap.val);//doing this returns a strange function thing in the logs
})
.catch(error => {
console.log("Error in catch: "+error)
response.status(500).send(error)
})
}