我试图将一个对象从一个模型传递到我的路线,这样我就可以完成我的登录系统,但我没有收到任何东西。
型号代码:
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但没有任何效果。我知道我必须在某处丢失某些东西,但在哪里?
这是来自res.json的回复
{
"success": true,
"token": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im5pY29sYXMuZ2FyY2lhIiwiaWF0IjoxNTA5ODE5NjMxLCJleHAiOjE1MDk5MDYwMzF9.ubHbqwOZznoeR0C-FkUoX83MoYPomk5SW1ThIA7ww74",
"user": {}
}
空用户当然应该是用户对象
首先,我使用的是forEach
,所以我必须传递一个索引(user.Item[0].username
),然后我发现只使用一个简单的{{1}我可以使用Dynamo DB查询一个项目功能。
现在模特:
.get
顺便说一句,Dynamo DB将项目作为项目返回,所以我现在的路线是:
module.exports.getUserByUsername = function (user, callback) {
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'Users',
Key: {
"username": user
}
};
docClient.get(params, function (err, user) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
callback(null, user);
}
});
}
答案 0 :(得分:0)
我还认为您必须更改comparePassword以匹配以下更改
//change comparePassword
USER.comparePassword(password, user, (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'
})
}
});
答案 1 :(得分:0)
当您在回调中返回用户对象时,它不在范围内......
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) {
// user exists in this scope
console.log(user.username + ": " + user.password + user.email + user.firstName);
// something like...
if(user) return callback(null, user)
});
}
// user is undefined here
// callback(null, user);
});