1

I am trying to display an input area in a Jade file with pre-populated data gathered from the session info being stored in mongodb session store. The web app is built on Express for node.js. My Jade file looks like this with the input area (pre-populated with session data):

            input(name='username', value='#{username}')

So the input area is displaying the correct username stored in the session. Then, I want the user to be able to edit that field and submit a new username if desired. My mongodb update() looks like this:

uname = request.body.username;
targetcol.update({username: req.session.username, password: req.session.password}, {username: uname});

Once submitted however, the document in the mongodb collection for the related profile is not being updated, it's keeping the same value that's been pre-populated. I'm wondering if this is because I'm assigning it as a value= in the Jade file? Any advice on what is wrong and how to fix it?

4

2 回答 2

1

答案在 mongodb update() 方面。我显然没有回答我的问题......选择标准实际上是这样的:

input(name='username', value='#{username}', disabled)

我没有意识到disabled字段会传入未定义的值,我只是认为它使它们不可编辑。username所以 update() 没有工作,因为它在集合中找不到任何匹配的文档,因为undefined. 你知道他们对假设的看法!

不过,我会把答案归功于@Plato,调查这些值的提示是让我弄清楚这一点的原因。

于 2013-10-16T15:52:04.280 回答
1

假设您使用的是表单,请使用以下方法从您的路由处理程序函数中访问value带有名称的输入属性:usernamereq.body.username

app.use(express.bodyParser());
app.post('/form', function(req, res){
  var newUsername = req.body.username;
  targetcol.update({ username: req.session.uname, password: req.session.password}
  , {username: newUsername});
  res.redirect('/');
});
于 2013-10-16T15:34:02.047 回答