【问题标题】:Pass value from a html form to another (redirected page) through node API通过节点 API 将值从 html 表单传递到另一个(重定向页面)
【发布时间】:2020-08-04 17:19:06
【问题描述】:

我有表格 IDForm.html 和 registration.html

如果用户在 IDForm.html 中输入正确的 ID,则会重定向到 registration.html

id是mongodb中collection的objectID

在 mongooseDB 上的 ID 检查是在 nodejs API 上完成的

const checkID = async (req, res ,next){
    var ID = "5e9cca24beabb96a4caedc35" 
    User.findById(ID, 
        (err) => {
            if (err) return handleError(err);
            res.redirect('/api/users/'+ ID +'/register')        
        })

如何在加载了此 ID 的情况下重定向到注册页面。因此注册中的用户可以使用此 ID 在 mongodb 上更新他的集合

注册是一种更新 mongodb 上现有集合的方式

【问题讨论】:

  • 您是否收到错误消息?这在什么方面没有按预期工作?
  • 你在使用某种模板引擎吗?
  • 我没有使用模板引擎,index.html 被加载到 server.js 中,然后所有其他页面都从索引重定向到另一个页面,我不知道这是否是正确的选择,或者应该为每个页面使用 res.sendfile 或模板引擎

标签: javascript html node.js mongodb


【解决方案1】:

在注册端,路由应该能够从请求的URL中获取参数,并传递给前端。

假设这是注册路径:

router.get('/registration,(req,res)=>{

//You will have the access to the URL parameters here
//Just an example
console.log(req.params.id)


//Pass the id to the front end if you are using a templating
// engines like express or ejs
res.render('template',{

id:req.params.id

});

//Or you can just send it out using
res.send(req.params.id)

//And handle it in the front end as you like


});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多