I'm creating a cloud function to search for candidates in relation to some filters for a vacancy, basically I'm searching in an array in my "cadvaga" collection for the "education" field and comparing it with the "Candidate" collection in the simple "education" field, if the candidate has one of the educations in the "cadvaga" array, I want to generate a list with the references of these candidates and show them in a listview, this function in the firebase console is already working and brings me the answer I want, the problem is in the treatment of this return in flutterflow.
I just can't do it.
This is my cloud function below (code), In this function I pass a parameter that is the reference of the vacancy and I also created its return as an array of type string
when calling the cloud function :
1- I update a page state of type string which is a list
2- then generate a listview of this page state
3- and in the listview I added text to show each item in the list
but only on firebase console on registers i can see the results like this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
if (admin.apps.length === 0) {
admin.initializeApp();
}
exports.matchCandidatesToJob = functions.region('southamerica-east1')
.runWith({
memory: '128MB'
}).https.onCall(async (data, context) => {
const cadvagaReferencia = data.cadvagaReferencia;
try {
const db = admin.firestore();
const jobDoc = await db.collection('cadvaga').doc(cadvagaReferencia).get();
if (!jobDoc.exists) {
console.log('Vaga não encontrada.');
return { candidateRefs: [], message: 'Vaga não encontrada.' };
}
const jobData = jobDoc.data();
const jobEscolaridade = jobData.escolaridade || [];
const candidatesSnapshot = await db.collection('Candidato').get();
const matchingCandidates = [];
candidatesSnapshot.forEach((doc) => {
const candidateData = doc.data();
const candidateEscolaridade = candidateData.escolaridade;
if (jobEscolaridade.includes(candidateEscolaridade)) {
matchingCandidates.push(doc.ref.path);
}
});
console.log('Candidatos correspondentes encontrados:', matchingCandidates);
return { candidateRefs: matchingCandidates };
} catch (error) {
console.error('Erro ao buscar candidatos correspondentes:', error);
throw new functions.https.HttpsError('unknown', 'Erro ao buscar candidatos correspondentes', error);
}
}).catch(error => {
console.error('Erro ao executar a função:', error);
});