skip to Main Content

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

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

2

Answers


  1. 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
      }
    }
    
    Login or Signup to reply.
  2. I see two problems in your code.

    First you don’t await for the loadDataPesan to return anything.

    This means that when

    console.log(dataaxio); 
    

    Is called, the promise hasn’t resolved yet. Making the function where this is called an async function will allow you to add await before the call of the function loadDataPesan. This will stop the execution of your code until the promise in loadDataPesan is resolved

     async (req, res) => {
        const contacts  = loadContact();
        const dataaxio = await loadDataPesan("SUBS","912830123");
     // ...
    

    Secondly, you don’t actually return anything in loadDataPesan. You do have a return statement in the callback of your promise (the function inside the then method), but you don’t return the promise at all.

    To fix that you just need to add a return before the call of axios

    const axios = require('axios');
    
    
    loadDataPesan=async function(opts){
        return axios.get('localhost/getData', {
            params: {
              opt: opts,
              nis: "123123",
            }
          })
         // ...
    }
    
    module.exports = { loadDataPesan };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search