1

I'm following this (YouTube Link) node tutorial cause I have to build a web app for a friend. He need to use Dynamo DB and node js and I'm learning while I build this app, I was doing great but now I'm stuck.

I'm trying to get a JWT token (as mentioned on the video) but nothing happens. Here's my route:

router.post('/authenticate', (req, res, next) => {
  const username = req.body.username;
  const password = req.body.password;

  USER.getUserByUsername(username, (err, user) => {
    if (err) throw err;
    if (!user) {
      return res.json({
        success: false,
        "msg": "User not found"
      });
    }
    User.comparePassword(password, user.password, (err, isMatch) => {
      if (err) throw err;
      if (isMatch) {
        const token = jwt.sign(user, secret.secret, {
          expiresIn: 86400
        });
        res.json({
          success: true,
          token: 'JWT ' + token,
          user: {
            user: user.username,
            password: user.password,
            email: USER.email
          }
        });
      }
    });
  });
});

Here's the passport js code

module.exports = function (passport) {
  let opts = {};
  opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
  opts.secretOrKey = secret;
  passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
    USER.getUserbyUsername(jwy_payload.user2, (err, user) => {
      if (err) {
        return done(err, false);
      }
      if (user) {
        return done(null, user);
      } else {
        return done(null, false);
      }
    });
  }));
}

And here's the function in the model user.js

module.exports.getUserByUsername = function (user, callback) {
  var docClient = new AWS.DynamoDB.DocumentClient();

  var params = {
    TableName: "Usuarios",
    KeyConditionExpression: "#us = :uuuu",
    ExpressionAttributeNames: {
      "#us": "username"
    },
    ExpressionAttributeValues: {
      ":uuuu": user
    }
  };

  docClient.query(params, function (err, data) {
    if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
      data.Items.forEach(function (item) {
        console.log(item.username + ": " + item.password);
        let user = item.username;
        console.log(user);
        return user;
      });
    }
  });
}

That last function prints the username + the password, so the query is working but that's it, I got no response at all after passing the username and password through Postman.

Thanks guys!

Kalana Demel
  • 3,220
  • 3
  • 21
  • 34
  • 1
    What are you expecting `return user` to do inside `data.Items.forEach()`? That `return` is only returning from the `.forEach()` callback, not from the outer function itself. – jfriend00 Nov 02 '17 at 00:26
  • 1
    You do not call the callback that is passed to `getUserByUsername` anywhere. – t.niese Nov 02 '17 at 00:28
  • Tried moving the return outside the forEach, nothing changed :( –  Nov 02 '17 at 00:29
  • Excuse me @t.niese I'm still new with all this coding stuff, can you develop your answer please? –  Nov 02 '17 at 00:31
  • 1
    Possible duplicate of [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – t.niese Nov 02 '17 at 00:34
  • You have to call `callback` after your forEach with the data you want to pass. – t.niese Nov 02 '17 at 00:36
  • Ok, now I do understand the basic concept of callback. Basically I have to create a function that pass the param to getUserByUsername? –  Nov 02 '17 at 00:45

1 Answers1

0

It seems that you are not calling callback

  docClient.query(params, function (err, data) {
    if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
      data.Items.forEach(function (item) {
        console.log(item.username + ": " + item.password);
        let user = item.username;
        console.log(user);
        return user;
      });
    }
  });

Replace the following one with above code

  docClient.query(params, function (err, data) {
    if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
      data.Items.forEach(function (item) {
        console.log(item.username + ": " + item.password);
        let user = item.username;
        console.log(user);
        callback(user);
      });
    }
  });