【问题标题】:Using Cloud Functions to filter data by string from cloud firestore使用 Cloud Functions 按字符串从 Cloud Firestore 过滤数据
【发布时间】: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


    【解决方案1】:

    我得到一个结果,我使用 onCall 函数来触发它。这是来自云函数的代码:

    exports.filterAds = functions.https.onCall( async (data, context) => {
      let vm = this;
      let filteredResults = [];
      let {filter,id_client} = data;
      let regex = new RegExp(filter, 'i');
        await 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())
            }
          })
        })
        return filteredResults;
    
    });
    

    下面是触发它的函数:

        filter_Ads: function () {
          const vm = this;
          const filteredValues = firebase.functions().httpsCallable("filtrarAnuncios");
    
    
          valoresFiltrados({
            filter: *input string*,
            id_cliente: *part of path in my firebase search*,
          })
            .then(async (response) => {
              console.log(respose)
            })
            .catch((error) => {
              console.log(error);
            });
        },
    
    

    有了这个,我可以通过正则表达式过滤并从此云函数中恢复结果。

    【讨论】:

      【解决方案2】:

      是的,您可以指定 nodejs 的版本并使用处理 async / await 的现代版本。

      你应该在你的.then(snapshot => {...}) 中调用res.send 而不是之后,因为这里你在从数据库中获取数据之前调用它

      同样在你的测试中,你应该使用await console.log(response) 而不是console.log(await response.text())

      您的代码还有其他一些令人惊讶的地方,例如您如何只是 .toString 数组而不是将其发送为 JSON

      【讨论】:

      • 我还是个初学者,谢谢你的帮助!我仍然在使用 ajax fetch() 时遇到一些问题。我有这个日志“TypeError:无法在'Window'上执行'fetch':使用GET / HEAD方法的请求不能有正文。”你知道如何处理这个错误吗?
      • 没问题,坚持下去,这是一个你可以通过一些搜索了解的错误,如果你希望你的请求包含一个正文,你应该使用http方法POST而不是默认GET。谷歌它以获得更完整的答案:)
      猜你喜欢
      • 2021-03-29
      • 2018-03-23
      • 1970-01-01
      • 2020-10-03
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多