Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AXIOS in express js return Promise { undefine }

i new learn expressjs and axios

i create a folder utils and place the
axios.js file

const axios = require('axios');


loadDataPesan=async function(opts){
    
    axios.get('localhost/getData', {
        params: {
          opt: opts,
          nis: "123123",
        }
      })
      .then(function (response) {
        console.log(response.data.datas) // ITS NORMALLY SHOW THE CORRECT DATA 
        return response.data.datas; // HERE MY PROBLEM, WHEN I RETURN IT DATA always Promise { }
      })
      .catch(function (error) {
        return error; 
      });
}

module.exports = { loadDataPesan };

here my app.js files

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const express = require('express');
const expressLayout = require('express-ejs-layouts');
const { loadContact } = require('./utils/contact');
const { loadDataPesan } = require('./utils/axios');
const app = express();
const port = 8899;

app.set('view engine', 'ejs');
app.use(expressLayout);

// BUILTIN MIDDLEWARE
app.use(express.static('public'));

app.get('/contact', (req, res) => {
    const contacts  = loadContact();
    const dataaxio = loadDataPesan("SUBS","912830123");

    
    console.log(dataaxio); // **HERE MY PROBLEM RESPONSE ALWAYS Promise { }**
    
    res.render('contact', { 
        layout: 'layouts/main-layout',
        title: 'Halaman Contact',
        contact:contacts,
    });
})

thanks you in advance if someone can help me

i try given async in function but it same also i try callback response its same also

>Solution :

Use async/await to wait for the result. You may need to catch the error.

app.get('/contact', async (req, res) => {    <-- convert it to async function
    const contacts  = loadContact();
    const dataaxio = await loadDataPesan("SUBS","912830123");  <-- await for response

    
    console.log(dataaxio); 
    
    res.render('contact', { 
        layout: 'layouts/main-layout',
        title: 'Halaman Contact',
        contact:contacts,
    });
})

and return the data from loadDataPesan function as

loadDataPesan = async function(opts) {
  try {
    const response = await axios.get('localhost/getData', {
      params: {
        opt: opts,
        nis: "123123",
      }
    });

    console.log(response.data.datas); // Log the correct data
    return response.data.datas; // Return the data

  } catch (error) {
    console.error(error);
    throw error; // Throw the error to be caught by the caller
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading