无法在功能外发送响应

时间:2019-07-11 00:35:11

标签: javascript arrays variables google-cloud-firestore google-cloud-functions

const admin = require('firebase-admin');
const functions = require('firebase-functions');

admin.initializeApp(functions.config().firebase);
let db = admin.firestore();
var dat=[];
var dat1=[];

function getDocument() {
  let cityRef= 
  db.collection('users').get()
  .then(function(snapshot){


    var i=0;
    snapshot.forEach(function(doc){
      //dat[i]=doc.id;
     // i++
      db.collection('users').doc(doc.id).collection('profile').doc(doc.id).collection("pet").doc(doc.id).get().then((snapshot) => {  
             //console.log(doc.id, '=>', doc.data());

              dat1.push(snapshot.data()['bio']+"");    
              console.log('details:' +dat1);
         //response.send(dat);
         return snapshot;

    })
    .catch((err) => {
      console.log('Error getting documents', err);
     });
  });
    return snapshot; 
 }).catch((err) => {
   console.log('Error getting documents', err);
  });

  return dat1;
}   

  exports.helloWorld = functions.https.onRequest(function(request, response){
    response.send(getDocument());
});
db.collection('users').doc(doc.id).collection('profile').doc(doc.id).collection("pet").doc(doc.id).get().then((snapshot) => {  
                  dat1.push(snapshot.data()['bio']+"");    
                  console.log('details:' +dat1);
             response.send(dat);
             return snapshot;``

        }).catch((err) => {
        console.log('Error getting documents', err);``
         });
      });``

这是用于Firebase云功能。我是Firebase和javascript的新手。我无法在函数外部访问数组,但只能在函数内部访问。我只能在这里得到一个响应,但是我需要一个完整的数组。

0 个答案:

没有答案