【问题标题】:Express.js create URLs from MongoDB IDExpress.js 从 MongoDB ID 创建 URL
【发布时间】:2014-09-22 23:57:30
【问题描述】:

我是 node.js 和 express.js 的新手。我正在尝试创建一个像这样工作的待办事项应用程序: 我有一个包含单个待办事项列表的待办事项 mongodb。这些列表将任务分类为未完成或已完成。例如:

{
Todos: {
    Todo: {
        "_id" : ObjectId("5202b481d2184d390cbf6eca"),
        finished: ['walk dog', 'do dishes'],
        unfinished: ['clean room']
    }
    Todo: {
        "_id" : ObjectId("5202b49ad2184d390cbf6ecb"),
        finished: ['clean car', 'make dinner'],
        unfinished: ['write this damn web app ']
    }
}
}

我想为每个待办事项列表创建新页面,以便列表的位置如下:http://website.com/5202b49ad2184d390cbf6ecb

所以我的问题是,我怎样才能像这样动态创建 URL?

谢谢!


编辑:

我将此代码添加到 routes/index.js:

/* POST to New todo list service */
router.post('/:id', function(req, res) {
    var db = req.db;
    var tasks = db.get('taskscollection');
    tasks.insert({
        "id":req.params.id,
        "finished":[""],
        "unfinished":[""]
        }, function(err, doc) {
            if (err) {
                res.send("there was a problem with adding a new tasklist");
                }
            else {
                res.location("/:id");
                res.redirect("/:id");
                }
            });
    });

这个代码到 index.jade:

   form#formNewTask(name='newtask', method='post', action='/:id')
     button#btnSubmit(type="submit") New Task List

但是当我点击按钮时,我被重定向到 localhost:3000/:id 并且我得到一个 404 未找到。我在这里错过了什么?

另外我如何为新页面创建翡翠模板?


编辑 2:

我将 index.js 更改为如下所示:

/* POST to New todo list service */
router.post('/tasks/:_id', function(req, res) {
    var db = req.db;
    var tasks = db.get('taskscollection');
    tasks.insert({                                        
        "finished":[""],
        "unfinished":[""]
        }, function(err, doc) {
            if (err) {
                res.send("there was a problem with adding a new task\
list");
                }
            else {
                res.location("/tasks/"+req.params.id);
                res.redirect("/tasks/"+req.params.id);
                }
            });
    });

我的 index.jade 现在有了这个:

   form#formNewTask(name='newtask', method='post', action='/tasks/'+_id)
     button#btnSubmit(type="submit") New Task List

现在,当我单击新任务时,它会将我带到 localhost:3000/tasks/undefined。这是否表明它没有在数据库中创建新条目?我认为我的前端jade文件是错误的,但我不确定。

【问题讨论】:

    标签: javascript node.js mongodb express pug


    【解决方案1】:
    app.get('/task/:id',
          function(req,res){
             //access the id by req.params.id
             //and use it to obtain data from mongodb
    
          });
    

    查看req.params

    当您发布数据时,尚未创建对象。因此没有_id。我的建议是在将新对象发布到数据库时不要使用参数,只需使用 '/posttask/' 之类的东西

    app.post('/postnewtask/',...
    

    而且您没有为“/task/:id”编写get 调用。你只是在写一个post 电话。 你得到的应该是

    app.get('/task/:id'...
    //use req.params.id and get data from database and render
    

    【讨论】:

    • 谢谢!我试图将其合并,但我仍然有点迷茫。我更新了我的问题,你能帮忙吗?
    • 不要使用 /:id,而是使用 //:id 之类的东西。从前端你应该使用 //232332332。您当然会动态生成该数字。可惜我对玉一无所知。
    • 那么这个数字是来自 Math.random(或其他东西)还是 Mongo 创建它并把它还给我?
    • 不!那个字符串就是你问的。正如你所说,website.com/<something>/5202b49ad2184d390cbf6ecb 之类的东西。因此,要动态生成它,请添加字符串,例如 '//'+data._id。我假设它是文档的_id
    • 是的,它是文档的 _id。好的,我会进行这些更改,然后看看。再次感谢!
    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 2011-12-18
    • 2016-12-12
    • 2016-08-25
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多