У меня есть две функции в Firebase. Первый в порядке:
exports.sendPush = functions.database.ref('/itemADividir/{itemId}').onCreate((snapshot, context) =>{
const oque = snapshot.val();
const quem = oque.userOrigem;
const itemADV = oque.itemAdividir;
const quanto = oque.itemValor;
return pegaTokenUser(oque.userDestino).then(userTk =>{
let payload ={
notification: {
title: 'Solicitação - Notificação InfSocial',
body: `${quem} quer compartilhar ${itemADV} no valor de ${quanto} com vc`,
sound: 'default',
badge: '1'
}
};
console.log(`${quem} quer compartilhar ${itemADV} no valor de ${quanto} com vc`)
console.log(`token ${userTk}`)
return admin.messaging().sendToDevice(userTk, payload)
})
})
Второй возвращается неопределенным в userRecusado:
exports.itemRecusado = functions.database.ref('/ItemDividirNegado/{itemId}').onCreate((snapshot, context) => {
const itemId = context.params.itemId;
const oRecusado = snapshot.val();
const userRecusado = oRecusado.userOrigem;
console.log(`item ${itemId} recusado ${userRecusado}`)
return pegaTokenUser(userRecusado).then(userTk =>{
let payload ={
notification: {
title: 'Solicitação - Notificação InfSocial',
body: `${oRecusado.userDestino} recusou compartilhar ${oRecusado.itemADV} com vc`,
sound: 'default',
badge: '1'
}
};
console.log(`${oRecusado.userDestino} recusou compartilhar ${oRecusado.itemADV} com vc`)
console.log(`token ${userTk}`)
return admin.messaging().sendToDevice(userTk, payload)
})
})
Вот функция для получения токена от пользователей:
function pegaTokenUser(userGet){
let userGet2 = userGet.replace ('.','')
let dbRef = admin.database().ref('/UserTokens');
let defer = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
let data = snap.val();
let userTk;
for (var user in data) {
if(data[user].user === userGet2){
userTk = (data[user].token);
}
}
resolve (userTk)
},(err) => {
reject(err);
});
});
return defer
}
Кто-нибудь знает, почему это происходит? Эти две функции выполняют одно и то же, но вторая не получает свойство созданной записи.
Полный файл:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.sendPush = functions.database.ref('/itemADividir/{itemId}').onCreate((snapshot, context) =>{
const oque = snapshot.val();
const quem = oque.userOrigem;
const itemADV = oque.itemAdividir;
const quanto = oque.itemValor;
return pegaTokenUser(oque.userDestino).then(userTk =>{
let payload ={
notification: {
title: 'Solicitação - Notificação InfSocial',
body: `${quem} quer compartilhar ${itemADV} no valor de ${quanto} com vc`,
sound: 'default',
badge: '1'
}
};
console.log(`${quem} quer compartilhar ${itemADV} no valor de ${quanto} com vc`)
console.log(`token ${userTk}`)
return admin.messaging().sendToDevice(userTk, payload)
})
})
exports.itemRecusado = functions.database.ref('/ItemDividirNegado/{itemId}').onCreate((snapshot, context) => {
const itemId = context.params.itemId;
const oRecusado = snapshot.val();
const userRecusado = oRecusado.userOrigem;
console.log(`item ${itemId} recusado ${userRecusado}`)
// the output
//itemRecusado
//item 1586992687992 recusado **undefined**
return pegaTokenUser(userRecusado).then(userTk =>{
let payload ={
notification: {
title: 'Solicitação - Notificação InfSocial',
body: `${oRecusado.userDestino} recusou compartilhar ${oRecusado.itemADV} com vc`,
sound: 'default',
badge: '1'
}
};
console.log(`${oRecusado.userDestino} recusou compartilhar ${oRecusado.itemADV} com vc`)
console.log(`token ${userTk}`)
return admin.messaging().sendToDevice(userTk, payload)
})
})
function pegaTokenUser(userGet){
let userGet2 = userGet.replace ('.','')
let dbRef = admin.database().ref('/UserTokens');
let defer = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
let data = snap.val();
let userTk;
for (var user in data) {
if(data[user].user === userGet2){
userTk = (data[user].token);
}
}
resolve (userTk)
},(err) => {
reject(err);
});
});
return defer
}
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
exports.TrocaEmoki = functions.database.ref('/Chat/{pathStr}/{tStamp}').onCreate((snapshot, context) =>{
const pathStr = context.params.pathStr;
const tStamp = context.params.tStamp;
console.log(`Nova mensagem ${tStamp} na mesa ${pathStr}`)
const mensagem = snapshot.val()
const text = addEmoji(mensagem.text)
return snapshot.ref.update({text})
})
function addEmoji(text) {
//return text.replace(/\bpizza\b/g, '?')
var regex1 = /pizza/,
regex2 = /hotdog/,
regex3 = /vinho/,
regex4 = /hamburguer/,
regex5 = /taco/,
regex6 = /fritas/,
regex7 = /sushi/,
regex8 = /churrasco/,
regex9 = /pipoca/,
regex10 = /peixe/,
regex11 = /cerveja/,
regex12 = /sorvete/,
alimento = text;
switch(true){
case regex1.test(alimento) :
return text.replace(/\bpizza\b/g, '?');
break;
case regex2.test(alimento):
return text.replace(/\bhotdog\b/g, '?');
break;
case regex3.test(alimento):
return text.replace(/\bvinho\b/g, '?');
break;
case regex4.test(alimento):
return text.replace(/\bhamburguer\b/g, '?');
break;
case regex5.test(alimento):
return text.replace(/\btaco\b/g, '?');
break;
case regex6.test(alimento):
return text.replace(/\bfritas\b/g, '?');
break;
case regex7.test(alimento):
return text.replace(/\bsushi\b/g, '?');
break;
case regex8.test(alimento):
return text.replace(/\bchurrasco\b/g, '?');
break;
case regex9.test(alimento):
return text.replace(/\bpipoca\b/g, '?');
break;
case regex10.test(alimento):
return text.replace(/\bpeixe\b/g, '?');
break;
case regex11.test(alimento):
return text.replace(/\bcerveja\b/g, '?');
break;
case regex12.test(alimento):
return text.replace(/\bsorvete\b/g, '?');
break;
default:
return text;
}
}