我当前正在使用的firebase函数从数据库中某个值可能具有或不具有百分比编码的分支中检索数据。该值是用户的用户名,如果有“。”则将被编码。在名字里。当用户收到通知时,它的名称会显示在其主体中,我正在尝试找出如何在必要时删除removePercentEncoding。我的云功能:
exports.newPost = functions.database.ref('/{school}/posts').onWrite((change, context) => {
const school = context.params.school
const postUsername = admin.database().ref('/{school}/lastPost/lastPostUser').once('value')
var db = admin.database();
var val1, val2;
db.ref(`/Herrick Middle School/lastPost/lastPostUser`).once('value').then(snap => {
val1 = snap.val();
console.log(snap.val());
return val1
}).then(() => {
return db.ref("test2/val").once('value');
}).catch(err => {
console.log(err);
});
return loadUsers().then(users => {
let tokens = [];
for (let user of users) {
tokens.push(user.pushToken);
console.log(`pushToken: ${user.pushToken}`);
}
let payload = {
notification: {
title: school,
body: `${val1} just posted something.`,
sound: 'Apex',
badge: '1'
}
};
return admin.messaging().sendToDevice(tokens, payload);
});
});
function loadUsers() {
let dbRef = admin.database().ref('/Herrick Middle School/regisTokens');
let defer = new Promise((resolve, reject) => {
dbRef.once('value', (snap) => {
let data = snap.val();
let users = [];
for (var property in data) {
users.push(data[property]);
console.log(`data: ${property}`);
}
resolve(users);
}, (err) => {
reject(err);
});
});
return defer;
}
更具体地说,我希望有人可以阐明如何从
删除编码val
谢谢。
答案 0 :(得分:0)
不确定我能理解,但是像这样的本机JS decodeURI()
或正则表达式
var encoded = "john%doe%doe%bird";
console.log(encoded.replace(/%/g, "."));