在将用户文档添加到mongo集合之后;如何在他们的个人资料页面上显示这个名字



我希望用户能够创建配置文件;然后被引导到显示其用户名的页面。

用户注册部分工作正常:

app.post('/users',
function (req, res) {
var hashedPassword = Users.hashPassword(req.body.Password);
Users.findOne({ Ainm: req.body.Name })
.then(function (user) {
if (user) {
return res.status(400).send(req.body.Name + "Name already in use.");
} else {
Users
.create({
Name: req.body.Name,
Password: hashedPassword,
Email: req.body.Email
})
.then(function (user) {
res.status(201).sendFile(path.join(__dirname, "public", "profile.html"));
}).catch(function (error) {
console.error(error);
res.status(500).send("Error: " + error);
})
}
}).catch(function (error) {
console.error(error);
res.status(500).send("Error: " + error);
});
});

但是,有人能建议如何将数据从服务器发送到客户端,以便在重定向到的配置文件页面上显示用户名吗?

客户端的html是这样的:

<div id="registration-menu">
<form action="/users" method="post">
<br />

<input type="text" name="Ainm"  placeholder="Ainm">
<br />
<input type="password" name="Password" placeholder="Passfhocal" >
<br />
<input type="email" name="Email" id="email" placeholder="seoladh r-post">
<br />
<br />
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Nuachtlitir
</label>
<br />
<input type="submit" class="btn btn-success btn-block" id="register" value="cláraigh">
</form>

您可以使用,

res.redirect('/users/${req.body.Name}')

并设置一个新的路由来显示用户的详细信息,使用他们的名称作为id来计算其他详细信息(重复的名称会导致冲突(。

使用jquery的.ajax((将数据从客户端发送到服务器端,如前所述:

$('#btnSendMsg').click(function () {
$.ajax('/addMsg', {
type: 'POST',  // http method
data: { myData: usrMsg },  // data to submit
success: function () {
usrMsg = $('#output').html()
// alert(usrMsg)
$('#output').fadeOut();
setTimeout(function () {
$('#output').empty();
$('#output').fadeIn();
}, 500)
}
});
});

服务器需要一个mongoose架构来与数据库进行通信。组件应该有一些实现数据存储的直通程序。用.json从服务器响应,表示最终的数据库。

>//client-side.js
> let gotIt = [];
> $.ajax('urlOfData', { dataType: 'json' }).then(function
> (response) {  gotIt = response;   //
> alert(JSON.stringify(response)) })

最新更新