Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用云函数从Cloud firestore按字符串筛选数据_Node.js_Ajax_Google Cloud Platform_Google Cloud Firestore - Fatal编程技术网

Node.js 使用云函数从Cloud firestore按字符串筛选数据

Node.js 使用云函数从Cloud firestore按字符串筛选数据,node.js,ajax,google-cloud-platform,google-cloud-firestore,Node.js,Ajax,Google Cloud Platform,Google Cloud Firestore,我需要按字符串过滤firestore数据,并且知道firestore在其工具(比如“%LIKE%”SQL操作符)中没有适当的方法来完成这项工作。我们的新策略是使用云函数使用正则表达式过滤此数据,然后恢复。 但我们遇到了一些麻烦: 1-我们可以在云功能内部管理同步功能吗 2-完成此功能后,我们如何恢复此数据?(我们正在尝试使用Fetch(),但它似乎不起作用。) 以下是我的云功能: const functions = require('firebase-functions'); const adm

我需要按字符串过滤firestore数据,并且知道firestore在其工具(比如“%LIKE%”SQL操作符)中没有适当的方法来完成这项工作。我们的新策略是使用云函数使用正则表达式过滤此数据,然后恢复。 但我们遇到了一些麻烦:

1-我们可以在云功能内部管理同步功能吗

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_comentary。下面是fetch函数:

    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)
      })
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;

});

我真的很难做到这一点,有人能帮我吗?

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

您应该在
内部调用
res.send
。然后(snapshot=>{…})
不要在之后调用,因为这里是在从数据库获取数据之前调用它

同样在测试中,您应该执行
console.log(wait-response.text())操作,而不是
wait-console.log(response)


您的代码还有一些其他令人惊讶的地方,比如您如何
.toString
数组而不是将其作为
JSON
发送。当我得到一个结果时,我使用onCall函数来触发它。以下是云函数的代码:

    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)
      })
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);
        });
    },


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

我还是一个新手,谢谢你的帮助!我在使用ajax fetch()时仍然遇到一些问题。我有这样一个日志“TypeError:未能在“窗口”上执行“fetch”:带有GET/HEAD方法的请求不能有body”。您知道如何处理此错误吗?没问题,请继续,如果您希望请求包含body,您可以通过搜索了解此错误,您应该使用http方法
POST
,而不是默认的
GET
。在谷歌上搜索一个更完整的答案:)好的,非常感谢kigiri!