路由器没有收到模型中的JSON?

时间:2017-11-04 00:43:12

标签: json node.js express passport.js

我试图将一个对象从一个模型传递到我的路线,这样我就可以完成我的登录系统,但我没有收到任何东西。

型号代码:

const AWS = require('aws-sdk');
const bcrypt = require('bcryptjs');
const config = require('../config/config.json');
var dynamoose = require('dynamoose');
const express = require('express');
var Schema = dynamoose.Schema;

const USER_SCHEMA = new Schema({
  username: {
    type: String,
    required: true
  },
  firstName: {
    type: String
  },
  lastName: {
    type: String
  },
  email: {
    type: String,
    required: true
  },
  credential: {
    type: String
  },
  password: {
    type: String,
    required: true
  }
})

const USER = module.exports = dynamoose.model('Usuarios', USER_SCHEMA);

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

  var params = {
    TableName: "Users",
    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 (user, callback) {
        console.log(user.username + ": " + user.password + user.email + user.firstName);
      });
    }
    callback(null, user);
  });
}

这工作正常,我可以打印user.username,user.password等,但由于某种原因我的路由器没有导入JSON

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({
          username: user
        }, secret.secret, {
          expiresIn: 86400
        });

        res.json({
          success: true,
          token: 'JWT ' + token,
          user: {
            user: user.username,
            password: USER.password,
            email: user.email
          }
        });
      } else {
        return res.json({
          success: false,
          msg: 'Wrong password'
        })
      }
    });
  });
});

来自comparePassword的res.json应该是路由中的对象(这是用户模型)但是没有收到任何东西。我尝试过USER.username / email / etc user.username / email / etc但没有任何效果。 我知道我必须在某处丢失某些东西,但在哪里?

编辑:还尝试使用模型中的module.export.user = user;

0 个答案:

没有答案