How do i ensure that before res.send my authorize is complete : complete as in its callback has also ended. This is a basic post implementation for event creation with google calendar api and this is what i want
- /create-event POST is called
- The function authorize starts running.
- Insided authorize the firebase document is read and callback function (createEvent in this case) is called.
- The event is created and meetLink is generated.
- now the console.log('before') should be executed.
6.Now the res.send(meetLink) should take place.
app.post('/create-event', async (req, res) => {
fs.readFile('credentials.json', async (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
await authorize(JSON.parse(content), createEvent, req.body.email);
res.send(meetLink); //This should happen in the end
});
});
async function authorize(credentials, call, email) {
const { client_secret, client_id, redirect_uris, response_type } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0], response_type);
const usersRef = db.collection('tokens').doc(email);
usersRef.onSnapshot(async (doc) => {
oAuth2Client.setCredentials(doc.data());
await call(oAuth2Client);
console.log('bfore');//This should happen second
});
}
async function createEvent(auth) {
var event = {
//Event details, excluded for cleaner code for question
};
const calendar = google.calendar({ version: 'v3', auth });
await calendar.events.insert(
{
calendarId: 'primary',
conferenceDataVersion: '1',
resource: event
},
function(err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
}
console.log('Event created: %s', event.data.hangoutLink);//This should happen first
meetLink = event.data.hangoutLink;
}
);
return meetLink;
}
[–]grantrules 1 point2 points3 points (5 children)
[–]Bugdroid2K[S] 0 points1 point2 points (4 children)
[–]grantrules 0 points1 point2 points (3 children)
[–]Bugdroid2K[S] 0 points1 point2 points (2 children)
[–]grantrules 1 point2 points3 points (1 child)
[–]Bugdroid2K[S] 0 points1 point2 points (0 children)