gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:25:37 25 4
gpt4 key购买 nike

我正在尝试将对象从模型传递到我的路线,以便我可以完成我的登录系统,但我没有收到任何内容。

型号代码:

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),然后我发现我可以查询使用简单的 .get 函数仅使用 Dynamo DB 获取 1 个项目。

现在的模型:

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
}
});
}

最佳答案

当您在回调中返回用户对象时,它不在范围内...

  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);
});

关于json - 将 JSON 从模型传递到路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com