如何使用Express和Mongoose发布和获取每个登录用户的唯一数据

How to Post and Get Data Unique to Each Logged In User with Express and Mongoose?

本文关键字:登录 用户 唯一 数据 获取 Express 何使用 Mongoose      更新时间:2023-09-26

我目前正在开发一个小的单页应用程序,允许用户登录PassportJs和Mongoose。

我正在尝试做的一件事是允许用户登录,每个用户都有一个唯一的todo/task列表,其中是与该用户相关的项目。

我已经能够做第一部分…用户可以使用jade #{user登录和访问express/passport会话。,所以当用户登录时看到"Welcome, [user.username]"

现在我添加一个表单(当用户登录时可访问),表单显示未定义。我不确定是我的Mongoose模式设计还是路由导致了这个问题。谢谢你的阅读,这是我的代码:

<<p> 猫鼬模式/strong>
mongoose.connect('mongodb://localhost/poplivecore')
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var user = new Schema({
username: String,
password: String,
email: String,
todos: [Todo]
});
var Todo = new Schema({
name: {type: String, default : ''},
user: {type: Schema.ObjectId, ref: 'user'},
createdAt  : {type : Date, default : Date.now}
})

var Todo = mongoose.model('Todo', Todo);
var user = mongoose.model('user', user);

这是我的快车路线://工作……这条路由是一个登录用户看到的,带有

的表单帖子
app.get('/home', ensureAuthenticated ,function(req, res){
res.render('home', { user: req.user});
});

//工作……此路由允许用户发布/提交登录

app.post('/login', 
passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
function(req, res) {
res.redirect('/home');
});

//WORKING....This route allows user to create a user/account    
app.post('/create', function(req, res, next){
var user = new user({
"username": req.body.username, 
"password" : req.body.password,
"email" : req.body.email});

user.save(function (err) {
if (!err) {
  res.redirect('/home');
}
else {
  res.redirect('/');
  }
 });
});

**//NOT WORKING..Post used in the form inside the logged in Area, that adds a 'todo'** 
app.post('/todo', function(req, res){
var todo = new todo(req.body.name);
todo.save(function (err) {
if (!err) {
  res.redirect('/home');
}
else {
  res.redirect('/fail');
  }
 });
});

翡翠表单,用于添加待办事项

enter code here
form(method='post', action='/todo')
 //input(type='hidden', value= user._id)#userId
 fieldset
  label Todo
   div.input
    input(name='todo.name', type='todo.name', class='xlarge')
   div.actions
    input(type='submit', value='Save', class='btn primary')
    button(type='reset', class='btn') Cancel

我可以张贴在github如果你需要看到更多的代码…谢谢。

根据'numbers1311407'建议更新*为todo添加新的post路由,也将todo在schema和routes中改为' todo ' *

app.post('/todo', function(req, res){
var todo = new Todo({name : req.body["Todo.name"]});

 todo.save(function (err) {
 if (!err) {
  res.redirect('/home');
 }
 else {
  res.redirect('/fail');
 }
 });
});

这里至少有两个问题会导致这个不能工作:

  1. 您的表单传递的输入名称是todo.name,并且您在路由中引用它为req.body.name

  2. 猫鼬模型是用一个属性对象实例化的,但你只是给它一个字符串(实际上,由于第一个问题,目前是null)。

因此,为了使您的路由工作,它看起来更像这样:

app.post("/todo", function (req, res) {
  var todo = new Todo({name: req.body["todo.name"]});
  todo.user = req.user._id;
  // ...
});

如果你想传递todo属性作为参数对象,你想用括号todo[name]来命名它们,而不是点。这将导致todo属性在请求的对象上。的身体,例如:

app.post("/todo", function (req, res) {
  console.log(req.body.todo); //=> { name: "whatever" }
  // ... which means you could do
  var todo = new Todo(req.body.todo);
  todo.user = req.user._id;
  // ...
});

您可能需要更改的其他内容:

  1. 正如@NilsH所指出的,您不希望在表单中传递用户id,因为这将允许任何人仅通过知道他们的id就可以为其他人制作待办事项。因为您使用的是passport,所以请在会话中使用该用户。您应该可以通过护照确定的用户访问用户ID,如req.user._id

  2. 表单输入的typetodo.name。它应该是text(这是浏览器如何处理它)。

  3. 不一定是错误,但是模型名称通常是大写的。这也解决了一个问题,你的代码在上面,你重新定义todo,当你说var todo = new todo(...)