首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Firebase云函数返回2个集合的数据

Firebase云函数返回2个集合的数据
EN

Stack Overflow用户
提问于 2019-09-19 19:14:47
回答 1查看 175关注 0票数 0

我已经编写了这两个云函数来在Cloud Firestore中发布数据,它们工作得很好。现在,我需要编写一个Cloud函数,该函数应该获取上面创建的两个集合的数据以发布数据。数据应该放在一个新对象中并合并在一起

所需JSON输出:

代码语言:javascript
运行
复制
[
    {
        "course": "xxx";
        "client": "hello";
        "location": "lahore";
        "instructor": "ahsan";
        "bidding": true;
        "price": "400";
        "totalHours": "15";
        "maxStudents": "30",
        "listing": true,
        "closeDays": "20",
        "assistants": "hassan",
        "publicNotes": "nothing to say",
        "internalNotes": "nothing to say",
        "course": "xxx",
        "date": "25/9/2019",
        "hour": "3",
        "minute": "15",
        "timeofday": "PM",
        "tohour": "6",
        "tominute": "15",
        "totimeofday": "PM"
    }
]

//排课

代码语言:javascript
运行
复制
exports.scheduleClass = functions.https.onRequest((req, res) => {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Methods', 'GET', 'POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type');

    if (req.method === 'OPTIONS') {
        res.end();
    }
    else
    {
        if(req.body.course != null && req.body.client != null && req.body.location != null && req.body.instructor != null && req.body.bidding != null
        && req.body.price != null && req.body.totalHours != null && req.body.maxStudents != null && req.body.listing != null && req.body.closeDays != null
        && req.body.assistants != null && req.body.publicNotes != null && req.body.internalNotes != null
        || req.body.course != undefined && req.body.client != undefined && req.body.location != undefined && req.body.instructor != undefined && req.body.bidding != undefined
        && req.body.price != undefined && req.body.totalHours != undefined && req.body.maxStudents != undefined && req.body.listing != undefined
        && req.body.closeDays != undefined && req.body.assistants != undefined && req.body.publicNotes != undefined && req.body.internalNotes != undefined) {
            let docId = Math.floor(Math.random() * (99999 - 00000));
            let newClass = {
                "course": req.body.course,
                "client": req.body.client,
                "location": req.body.location,
                "instructor": req.body.instructor,
                "bidding": req.body.bidding,
                "price": req.body.price,
                "totalHours": req.body.totalHours,
                "maxStudents": req.body.maxStudents,
                "listing": req.body.listing,
                "closeDays": req.body.closeDays,
                "assistants": req.body.assistants,
                "publicNotes": req.body.publicNotes,
                "internalNotes": req.body.internalNotes
            }
            usersClasses.add(newClass).then(snapshot => {
                res.send(200, {
                    "message": "Class was successfully created"
                })
            });
        } else {
            res.send(400, {
                "message": "All fields are required"
            })
        }
    }
});

//添加类的时间

代码语言:javascript
运行
复制
exports.addTimes = functions.https.onRequest((req, res) => {
  res.set('Access-Control-Allow-Origin', '*');
  res.set('Access-Control-Allow-Methods', 'GET', 'POST');
  res.set('Access-Control-Allow-Headers', 'Content-Type');

  if (req.method === 'OPTIONS') {
      res.end();
  }

  else {
    if(req.body.course != null && req.body.date != null && req.body.hour != null && req.body.minute != null
    && req.body.timeofday != null && req.body.tohour != null && req.body.tominute != null && req.body.totimeofday != null
    || req.body.course != undefined && req.body.date != undefined && req.body.hour != undefined && req.body.minute != undefined && req.body.timeofday != undefined
    && req.body.tohour != undefined && req.body.tominute != undefined && req.body.totimeofday != undefined)
    {
      let docId = Math.floor(Math.random() * (99999 - 00000));
      let newTimes = {
        "course": req.body.course,
        "tohour": req.body.tohour,
        "tominute": req.body.tominute,
        "totimeofday": req.body.totimeofday,
        "date": req.body.date,
        "hour": req.body.hour,
        "minute": req.body.minute,
        "timeofday": req.body.timeofday,
      }
      classtimesCollection.add(newTimes).then(snapshot => {
        res.send(200, {
          "message":"Time has been added"
        })
      });
    }
    else {
      res,send(400, {
        "message": "All fields are required"
      })
    }
  }
});

我能够从一个集合中获得数据,如下所示:

代码语言:javascript
运行
复制
exports.getClassesSchedule = functions.https.onRequest((req, res) => {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Methods', 'GET', 'POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type');

    if (req.method === 'OPTIONS') {
        res.end();
    }

    else
    {

        let allClasses = [];
        usersClasses.get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    allClasses.push(doc.data());
                });
                res.send(allClasses);
            })
            .catch(err => {
                console.log("Error getting documents", err);
            });
    }
});
EN

回答 1

Stack Overflow用户

发布于 2019-09-20 08:32:14

我想你已经很接近了,你只需要打另一个电话。在开始整理数据之前,您需要使用Promise.all来等待这两个承诺都被保证完成。

代码语言:javascript
运行
复制
exports.getClassesSchedule = functions.https.onRequest((req, res) => {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Methods', 'GET', 'POST');
    res.set('Access-Control-Allow-Headers', 'Content-Type');

    if (req.method === 'OPTIONS') {
        res.end();
    }

    else
    {
         let classTimes = []
         let promise1 = classTimesRef.get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    classTimes.push(doc.data());
                });
                return classTimes
            })
            .catch(err => {
                console.log("Error getting documents", err);
            });
        let allClasses = [];
        let promise2 = usersClassesRef.get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    allClasses.push(doc.data());
                });
                return allClasses
            })
            .catch(err => {
                console.log("Error getting documents", err);
            });
        return Promise.all([promise1, promise2]).then(data => {
            let times = data[0]
            let classes = data[1]
            // Collate the data here

            return res.send()
        }
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58009753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档