why my Nodejs / NestJs promise is returning undefined to variable in second function?

hamza_dilber

I have 2 functions. when I called 1st function promise from second function it prints the output correct but my 'newdata' variable in second function is returning undefined value. Please help.

My functions are:

async keycloaktokennew(data: any):Promise<any>
  {      
     return  await this.httpService.axiosRef.post(
      `http://localhost:8080/auth/realms/master/protocol/openid-connect/token`,
      querystring.stringify({
        username: 'stdev', //gave the values directly for testing
        password: 'admin123',
        grant_type: 'password',
        client_id: 'admin-cli',
      }),
      {
        headers: { 
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }
      ).then(function(response) {       
        console.log(response.data);     
    }); 
    
  }


  async newKeyToken(data: any){
    const newdata = await this.keycloaktokennew(data);
    if (newdata=="undefined") {
      throw new BadRequestException(INVALID_CREDENTIALS);
    }
    else{
    console.log("-------------------this is Result needed var-----------------");
    console.log(newdata);
    console.log("-------------------this is Result needed var-----------------");
    return newdata;
    }
  }
keinsell

if (newdata=="undefined") should be (!newdata), as in current flow it expect incoming "undefined" from previous function which is undefined because you do not return any information from callback.

    ).then(function(response) {       
        console.log(response.data);     
    });

Additionally you should return data from your promise.

    ).then(function(response) {       
        console.log(response.data);
        return response.data
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why does my nodejs function returning undefined?

Why is my promise returning undefined?

Promise returning undefined in nodejs

My Nodejs function is returning an Undefined instead of a value

Promise returning undefined variable

Why is my function returning Promise { <pending> }

Why is my second function not returning anything?

Why is this promise returning undefined?

Why is my variable tabledata returning undefined?

Why is my variable returning undefined in javascript?

Why is my recursive function returning undefined?

Why is my function returning an array with undefined as items?

Why is the variable argument of this required function returning undefined?

Nodejs - Why my variable in class is undefined?

Why is my function returning a Promise { <pending> } instead of my entries?

Why is my function returning 1 and not the variable value?

Why function not returning promise 'Cannot read property 'then' of undefined'?

Why is Mocha returning undefined when testing function that uses a promise?

Why is 'new Promise(...)' returning 'undefined'?

resolve() function is returning undefined in a promise

Function is returning object Promise - nodejs

why is this function returning undefined?

Why is my asynchronous function returning Promise { <pending> } instead of a value?

Why is my asynchronous function returning Promise { <pending> } instead of a value?

Why is my email check variable returning an error as undefined

Why is my variable returning as 'undefined' despite being set a value?

My variable is always undefined, function returning before awaiting the query

Why is my function returning undefined along with the reversed string

Why is my function returning undefined despite printing out correctly