gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:03:58 25 4
gpt4 key购买 nike

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

我正在尝试做的一件事是允许用户登录,每个用户都有一个唯一的待办事项/任务列表,这些列表是与该用户关联的项目。

我已经能够完成第一部分...用户可以登录并使用 jade #{user.username} 访问 express/passport session ,因此当用户登录时看到“欢迎,[user.username]” .

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

Mongoose 模式

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

这是我的 express 路线: //WORKING....这条路线是登录用户看到的路线,形成帖子

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

Jade Form,用于添加待办事项

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 的新发布路由,在架构和路由中也将 todo 更改为“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. mongoose 模型是用一个属性对象实例化的,但您只是给它一个字符串(实际上,由于第一个问题,该字符串当前为 null)。

所以对于你的上类路线来说,它看起来更像这样:

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

如果您想将 todo 属性作为参数对象传递,您需要用方括号 todo[name] 来命名它们,而不是点。这将导致待办事项属性位于 req.body 上的对象上,例如:

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 就可以为其他任何人创建待办事项。而是因为您使用的是护照,所以请在 session 中使用用户。您应该可以通过护照确定的用户访问用户 ID,例如 req.user._id。我将此添加到上面的两个示例中。

  2. 表单输入的类型todo.name。它应该是 text(无论如何浏览器都会将其视为文本)。

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

    <

关于javascript - 如何使用 Express 和 Mongoose 发布和获取每个登录用户的唯一数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256193/

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