Async / Await Request Inside Passport Strategy

I've searched everywhere for an answer that solves this problem and tried different suggestions, but I still can't figure it out.

I am using the Steam API strategy with passport, after creating my new SteamStrategy I have a callback function that handles my MongoDB collections. I just check if the current user is in the database, if not then create a new user and .save () in mongoDB.

This all worked fine until I decided I wanted to make a request () to the steam API for more user information that I can save to a new user.

However, when I try to save the request () to a variable and console.log (), it always says null. I know this because it works asynchronously, but that's why I used wait but didn't have time. I also tried to implement a solution like callback-hell but ran into some serious problems with my final "return done (null, user)" statement;

Anyway. If anyone could explain and provide a solution to my problem, that would be great, thanks!

passport.use(
      new SteamStrategy(
        {
          returnURL: keys.returnURL,
          realm: keys.realm,
          apiKey: keys.steamAPI,
          proxy: true
        },
        async function(identifier, profile, done) {
          const existingUser = await User.findOne({
            steamInfo: { id: profile._json.steamid }
          });

          if (existingUser) {
            middleware.updateMongo(existingUser);
            return done(null, existingUser);
          }

          ////////////////////////THE PROBLEM STARTS HERE/////////////////////
          const steamURL = "hiding url for post";

          const info = await request(steamURL, function(error, response, body) {
            if (!error && response.statusCode == 200) {
              console.log(JSON.parse(body)); //THIS PRINTS OUT THE INFO PERFECTLY
              return JSON.parse(body);
            }
          });


          console.log(info); //THIS WILL ALWAYS SHOW AS NULL OR UNDEFINED

          const user = await new User({
            steamInfo: {
              id: profile._json.steamid,
              persona: info.personaname,
              profileUrl: info.profileurl,
              avatar: info.avatarmedium,
              personaState: info.personastate,
              visibility: info.communityvisibilitystate,
              countryCode: info.loccountrycode
            },
            collectedInfo: { rank: "", reports: "0" }
          }).save();

          return done(null, user);
        }
      )
    );

      

+3


source to share


1 answer


The problem is that you are using await

both the callback at the same time. You should not. Also, the module request

doesn't use promises - you need request-promise

instead, which is basically a wrapper around the module request

so you can work with promises.



const request = require("request-promise");

let info = {};
try {
  info = await request(steamURL);
  console.log(info);
}
catch (err) {
  /* Handle error */
}

      

+2


source







All Articles