Home > Enterprise >  Promise keeps returning Undefined even using Await
Promise keeps returning Undefined even using Await

Time:12-22

I'm kinda new to Node.Js, I'm trying to make a chatbot with Spotify integration for my WhatsApp Group (I'm using wa-automate and spotify-web-api-node for this), I made a service layer for each command and a Spotify repository to search for albums, tracks, etc, but when I try to read albumList that getAlbumByName returns it is always Undefined even though it works just fine when I print it with console.log() in the SpotifyRepository, things I already tried:

  • using function.then(), returning inside of it then returning the function itself with no success, something like return function.then({(result) => {return result})}
  • not using await in this.spotifyApi.searchAlbums()
  • using function.then() inside the service layer
  • Trying to read the value outside the wa-automate client.sendText() function
  • I checked and searchAlbums() returns a Promise that should return the response when awaited

I suspect that authenticating SpotifyWebApi inside a middleware or my entire thought process surrounding the layer organization might be the problem.

Here is my code:

const SpotifyRepository = require('../repositories/spotifyRepository.js');

module.exports = class command {
    constructor() {
      this.name = 'getAlbum';
      this.aliases = ['getAlbum'];
      this.description = '>getAlbum';
      this.spotifyRepository = new SpotifyRepository();
    }
    
    async run(client, message, args) {
      let albumName = args.split(' - ')[0];
      let artistName = args.split(' - ')[1];
      let albumList = await this.spotifyRepository.getAlbumByName(albumName, artistName);
      client.sendText(message.from, albumList.items[0]);
    }

};

var SpotifyWebApi = require('spotify-web-api-node');

class SpotifyRepository {
    constructor() {
        this.spotifyApi = new SpotifyWebApi({
            clientId: '*************************',
            clientSecret: '*************************'
        });
        const handler = {
            get: function (obj, prop) {
              return typeof obj[prop] !== "function"
                ? obj[prop]
                : async function (...args) {
                    await obj.checkAuth(prop);
                    obj[prop].apply(obj, args);
                  };
            },
          };
        return new Proxy(this, handler);
    };

    async checkAuth(){
        let token = await this.spotifyApi.clientCredentialsGrant();
        this.spotifyApi.setAccessToken(token.body['access_token'])
    };

    async getAlbumByName(albumName, artistName) {
        return await this.spotifyApi.searchAlbums(albumName   ' artist:'   artistName);
    }
};

module.exports = SpotifyRepository;

Thanks for the help! (and sorry if this is confusing to read, english is not my first language)

CodePudding user response:

async function (...args) {
  await obj.checkAuth(prop);
  obj[prop].apply(obj, args);
};

Inside the proxy, you're making it so anyone who tries to access a function gets this function instead. But this function doesn't have a return statement, so it is implicitly returning a promise that resolves to undefined. You should add a return statement which forwards along whatever the real function returns:

async function (...args) {
  await obj.checkAuth(prop);
  return obj[prop].apply(obj, args);
};
  • Related