Я написал эти 2 облачные функции для публикации данных в облачном хранилище, и они работают нормально.Теперь мне нужно написать облачную функцию, которая должна получать данные обеих коллекций, которые были созданы выше, для публикации данных.Данные должны находиться внутри нового объекта и объединяться вместе
Требуемый вывод JSON:
[
{
"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"
}
]
// Планирование класса
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"
})
}
}
});
// Добавить время для классов
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"
})
}
}
});
Я могу получить данные из одной коллекции, например:
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);
});
}
});