Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How to get all data after callback method?

I am using node js to get all contact data from Google API. USing callback function but it takes some time to get data due to run function before next function. I am very new in node JS can someone help me to sort it out. I want all data return once the callback function is called.

A contact function to get all contacts.

app.get('/contacts', async function(req, res){
  
  // Rendering our web page i.e. Demo.ejs
  // and passing title variable through it
    fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err);
    // Authorize a client with credentials, then call the Google Tasks API.
     await  authorize(JSON.parse(content), listConnectionNames);
     res.send(full_person_array)
  });
  

})

This one is autorize function that callback listConnectionNames fucntion.

function authorize(credentials, callback) {

  return new Promise((resolve, reject) => {
    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
        client_id, client_secret, redirect_uris[0]);
  
    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, (err, token) => {
      if (err) return getNewToken(oAuth2Client, callback);
      oAuth2Client.setCredentials(JSON.parse(token));
      callback(oAuth2Client, id);
      return resolve();

    });

  })
 
}


function listConnectionNames(auth) {
  const service = google.people({version: 'v1', auth});
  service.people.connections.list({
    resourceName: 'people/me',
    pageSize: 50,
    personFields: 'names,emailAddresses,phoneNumbers',
  }, (err, res) => {
    if (err) return console.error('The API returned an error: ' + err);
    const connections = res.data.connections;
    if (connections) {
      console.log('Connections:');
      connections.forEach((person) => {
        console.log(person);
        full_person_array.push(person);
      });
    } else {
      console.log('No connections found.');
    }
  });
}

How to get all data in contact without get delayed empty data.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can add returns instead of mapping the result array to fill an other array :

function authorize(credentials) {

    const {
        client_secret,
        client_id,
        redirect_uris
    } = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
        client_id, client_secret, redirect_uris[0]);

    // Check if we have previously stored a token.
    return fs.readFile(TOKEN_PATH, (err, token) => {
        if (err) return getNewToken(oAuth2Client);
        oAuth2Client.setCredentials(JSON.parse(token));
        return oAuth2Client;

    });
}


async function contact() {
    let auth = await authorize(credentials);
    let connections = await listConnectionNames(auth, id);
    console.log(connections); // full result
}

Does it helps you ?

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda