从模型传递JSON到路由不起作用

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

型号代码:

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 /电子邮件/ etc的用户名/电子邮件/等,但没有任何工作。 我知道我必须在某处丢失某些东西,但是在哪里?

这是res.json的响应

 { "success": true, "token": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im5pY29sYXMuZ2FyY2lhIiwiaWF0IjoxNTA5ODE5NjMxLCJleHAiOjE1MDk5MDYwMzF9.ubHbqwOZznoeR0C-FkUoX83MoYPomk5SW1ThIA7ww74", "user": {} } 

空的用户当然应该是用户对象


固定:

首先,我使用了forEach ,所以我必须传递一个索引( user.Item[0].username ),然后我发现我可以用一个简单的.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); } }); } 

顺便说一句,Dynamo DB将项目返回为Item,所以现在我的路线是:

 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.Item.username, password: user.Item.password, email: user.Item.email, firstName: user.Item.firstName, lastName: user.Item.lastName, credential: user.Item.credential } }); } 

       

网上收集的解决方案 "从模型传递JSON到路由不起作用"

当您在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 (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); }); 

我也认为你将不得不改变你的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' }) } });