Firebase nodejs 没有正确执行 return 功能

Firebase nodejs doesn't execute return function properly

我们试图通过将它们推入数组然后 returning 从我们的数据库中获取时间段。根据 firebase 日志,该数组确实得到了正确填充,但是该函数根本没有 return 正确地填充数据,即使我们看到数据被 returned。 基本上执行不到return语句

我们的目标是获取这张照片中的所有时间段。有没有什么巧妙的方法可以做到这一点?

exports.getTimeslots = functions.region('europe-west2').https.onCall((data, context) => {
  const uid = context.auth.uid;
  let array = [];
  if (!uid)
    throw new functions.https.HttpsError('no-userid', 'The requested user was not found');
  else
    return admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get().then(snapshot => {
      snapshot.forEach(async doc => {
        await admin.firestore().collection('users').doc(uid).collection('modules').doc(doc.id).collection('timeslots').where('length', '!=', -1).get().then(snapshot2 => {
          snapshot2.forEach(doc2 => {
            array.push(Object.assign(doc2.data(), {id: doc2.id, modID: doc.id}))
            console.log("identifier #1", array) 
          })
          console.log("Got outside");
        })
        console.log("Got more outside");
      })
      console.log("Got the most outside")
      return ({ data: array });
    });
  //console.log("I have escaped!")
})

这是您函数编写方式的问题。而不是

  return ({ data: array });

你的函数有时returns。

  admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get()

这本身就是一个承诺。您在 then 函数中链接异步。解决方案是让一切异步等待。

 const data = await admin.firestore().collection('users').doc(uid).collection('modules').where('name', '!=', '').get()

 

正如@Ragesh Ramesh 所说:“解决方案是让一切异步等待。”,我尝试使用数据结构和以下代码复制您的代码:

数据结构:

代码:

// firebase db
const db = firebase.firestore();

exports.getTimeslots = functions.region('europe-west2').https.onCall((data, context) => {  
    const getData = async () => {
        const uid = context.auth.uid;
        let array = [];

        if (!uid) {
            throw new functions.https.HttpsError('no-userid', 'The requested user was not found');
        } else {
            const modulesRef = db.collection('users').doc(uid).collection('modules');
            const modulesQuery = modulesRef.where('name', '!=', '');
            const modulesQuerySnap = await modulesQuery.get();
            const moduleDocuments = modulesQuerySnap.docs.map((doc) => ({ id: doc.id }));

            for (const moduleDocument of moduleDocuments) {
            const timeslotsRef = modulesRef.doc(moduleDocument.id).collection('timeslots');
            const timeslotsQuery = timeslotsRef.where('length', '!=', -1);
            const timeslotsQuerySnap = await timeslotsQuery.get();
            const timeslotDocuments = timeslotsQuerySnap.docs.map((doc) => ({ id: doc.id, data: doc.data() }));
            
                for (const timeslotDocument of timeslotDocuments) {
                    array.push(Object.assign(timeslotDocument.data, {id: timeslotDocument.id, modID: moduleDocument.id}))
                }
            }
            return ({ data: array });
        }
    }

    return getData()
    .then((response) => { 
        // console.log(response);
        return response;
    });
}

The Reference page for Firestore 显示快照上的 docs 属性。

根据 运行 代码,输出如下:

{
  data: [
    {
      length: 1,
      id: '8UIlspnvelEkCUauZtWv',
      modID: 'RmL5BWhKswEuMWytTIvZ'
    },
    {
      title: 'Modules',
      length: 120,
      day: 1,
      startTime: 720,
      id: 'E5fjoGPyMswOeq8mDjz2',
      modID: 'qa15lWTJMjkEvOU74N1j'
    },
    {
      startTime: 360,
      title: 'English',
      day: 2,
      length: 240,
      id: '7JHtPSO83flO3nFOc0aE',
      modID: 'qa15lWTJMjkEvOU74N1j'
    }
  ]
}