Why Async function in Next JS returns empty object in my Typescript code?

Chukwu Remijius

I'm having some problems with async function.

I have a class in my ../lib folder for processing data from an API website but i ran into problems when i try to load the api data in async function.

The async function dont even return anything, not even when i create object and try to return it inside the async function

All i get is {} empty object every time i call the function

Here is my code:

 private procurr(obj){
    // Here we start processing
    const mainObj = async function(ob) {
        var robj : any = [];
        var item = {};
        var i : number = 0;
        var total : number = 10;
        try{
            for(item in ob){
                let res2 = await axios.get('https://hacker-news.firebaseio.com/v0/item/'+item+'.json');
                const obji = await res2.data;
                var word : string = this.help.mode(this.help.words(obji.title));
                if(word.length > 0){
                    if(i < total){
                        robj[i] = (this.help.inArr(word, robj)) ? this.help.incScore(robj[i]) : {'word': word, 'score':1};
                        // increment
                        i++;
                    }
                }
            }
        }catch(error){
            console.error(error);
        }
        
        // return JSON.parse(robj);
        return ob;       
    }

    // exports.mainObj = mainObj;
    // Here we return
    // return obj;
    return mainObj(obj);
}

EDITED:

I'm calling the procurr(obj) from q1(obj) like so:

      // Here we create question question method
     public q1(obj) : any {
       // var topIDs = this.help.obj2Arr(obj);
       var last25 = this.help.last25(obj);
       // Here we return
       return this.procurr(last25);
       // return last25;
     }

I'm calling the q1(obj) method from getStaticProps() like so

     export async function getStaticProps() {
        const postClass = new Posts();
        // Here we fetch data
        const res = await axios.get('https://hacker- 
        news.firebaseio.com/v0/topstories.json');
        const obj = await res.data;
        // const obj = await JSON.parse(JSON.stringify(res.data));
        // Here we check route request
        const ob = JSON.stringify(postClass.q1(obj));

        // ob = JSON.parse(postClass.q2(obj));
        // ob = JSON.parse(postClass.q3(obj));

        return {
          props: { ob }
        }
    }

The API call returns a object containing posts ids from hackernews website

Jamiec

You have a string of async methods, but at the bottom you forgot to await it. If you try to JSON.stringify a Promise you will get an empty object

const p = new Promise(resolve => resolve({foo:"bar"}));

console.log(JSON.stringify(p)) // Not {foo:"bar"} as you might expect

The method you're calling it from is already async so just add an await

const ob = JSON.stringify(await postClass.q1(obj));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why require("angular") returns an empty object

Why extend a function with an empty object?

why does my async function returns undefine while working with mysql2?

Why my async function returns an undefined when i try to access it?

typescript: Class returns empty object

Javascript async function returns [object Promise]

Ensure my function returns mutated object as instanceof the same class typescript?

Why does my async function return an empty array

Why is my fs error returning an empty object in Node.js?

Problem where Jquery .ajax function returns object after it has already skipped to the next line of code

Typescript: Why is PromiseLike a possible type of this in an object in an async function?

Why my getElementsByTagName returns an empty NodeList?

My code returns an empty value for the entry box

jQuery.next() returns empty object

Why does my function only returns the first object from the array

Why is this function returns an empty array?

Playing with getter function in a js object, why my code doesn't work?

Typescript .push function adds empty object to my array

HTTPS Callable Function that returns async function returning empty object

Why does my async function that returns a Future<int> stall at await for each?

Why ? Object params of function returns undefined JS

Uninfinite async function execution in next js

Context with React Native hooks returns empty object/array on async function

Async function returns empty array

Why does my async js/nodejs function skip a big part?

Why is this async function returning empty on the first try, but then it returns everything?

Why passing function object to Object.keys() returns empty array?

JavaScript async function returns object Promise

Is it possible that async function returns object having "then" function