【发布时间】:2020-12-09 01:08:56
【问题描述】:
我需要按字符串过滤 firestore 数据,并且知道 firestore 在他的工具中没有适当的方法来执行此操作(例如“%LIKE%” SQL 运算符),我们的新策略是使用云函数使用正则表达式过滤此数据并在之后恢复。 但是我们遇到了一些麻烦:
1-我们可以在 Cloud Functions 中管理异步函数吗?
2-在这个函数之后,我们如何恢复这些数据? (我们尝试使用 Fetch(),但看起来它不起作用。)
这是我的云函数:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(MY CREDENTIALS)
exports.filterAdverts = functions.https.onRequest( (req, res) => {
let vm = this;
let filteredResults = [];
let {filter,id_client} = JSON.parse(req.body);
let regex = new RegExp(filtro, 'i');
admin
.firestore()
.collection('database')
.doc(id_client)
.collection('classifieds')
.where('active', '==', 1)
.get().then(snapshot => {
snapshot.docs.forEach(doc => {
if (doc.data().txt_comentary.match(regex)) {
filteredResults.push(doc.data())
}
})
});
res.send(filteredResults.toString())
});
如您所见,我需要通过正则表达式过滤变量 txt_commentary。这是获取函数:
filterAds: function(){
let filter = {filter:this.form_seach.filtro_comentary, id_cliente:this.id_cliente};
fetch('Cloud-function-URL', {
headers: {'Content-Type':'application/x-www-form-urlencoded'},
method: 'GET',
mode: 'no-cors'
}).then(async (response) => {
await console.log(response)
}).catch(error => {
console.log(error)
})
我真的很难做到这一点,有人可以帮助我吗?
【问题讨论】:
标签: node.js ajax google-cloud-platform google-cloud-firestore