Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将用户文档添加到mongo collection后;如何在他们的个人资料页面上显示该名称?_Javascript_Html_Mongodb_Express - Fatal编程技术网

Javascript 将用户文档添加到mongo collection后;如何在他们的个人资料页面上显示该名称?

Javascript 将用户文档添加到mongo collection后;如何在他们的个人资料页面上显示该名称?,javascript,html,mongodb,express,Javascript,Html,Mongodb,Express,我希望用户能够创建一个配置文件; 然后被引导到显示其用户名的页面 用户注册部分工作正常: app.post('/users', function (req, res) { var hashedPassword = Users.hashPassword(req.body.Password); Users.findOne({ Ainm: req.body.Name }) .then(function (user) {

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

用户注册部分工作正常:

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如下所示:







Nuachtlitir
你可以用

res.redirect('/users/${req.body.Name}'))
并设置一个新路由以显示用户详细信息,使用其名称作为id来找出其他详细信息(重复的名称将导致冲突)。

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

服务器需要一个mongoose架构来连接数据库。组件应该有一些实现数据存储的贯穿程序。 从服务器响应,使用.json表示最终的数据库

>//client-side.js

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

设置要保存的用户作为响应,并在客户端上阅读。谢谢。因此,类似于
Users.create({Name:req.body.Name,Password:hashedPassword,Email:req.body.Email})。然后(函数(user){res.send(req.body.Name);res.sendFile('./public/profile.html',{root:_dirname})
//这还不起作用。另外,对于糟糕的格式设置,很抱歉。你能分享你的客户端代码吗?是的,我已经在上面的问题中添加了这个,谢谢。谢谢。我不太明白。这将返回一个带有“未经授权”的白色屏幕。我猜这表明mongoDb存在身份验证问题;但这很奇怪,因为我可以用res.json(user)@RoibeárdMacUnfraidh返回所有用户数据。你是否创建了新的路由,比如
app.get(“/users/:userName”,(req,res)=>{//pass required details in res})
谢谢,我正在尝试。我还有一点书要读。
$('#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)
      }
  });
});
>//client-side.js

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