了解新的mongo id并将其与iron-router一起使用

了解新的mongo id并将其与iron-router一起使用

问题描述:

我有一条简单的帖子路线,用于查找帖子_id. 问题是pathFor助手创建了这样的路径:

i have a simple post route that looks for the post _id. The problem is that the pathFor helper creates a path like this:

ObjectID("52e16453431fc2fba4b6d6a8")

我猜想mongoDB插入已被更改,现在_id对象在其中包含另一个名为_str的对象.

I guess the mongoDB insertion as been changed and now the _id object holds another object inside it called _str.

这是我的路线:

this.route("post", {
        path: "/post/:_id",

        waitOn:function(){
            NProgress.start();
            Meteor.subscribe("Teams");
        },

        before: function () {
            NProgress.done();
        },

        data: function () {
            return Posts.findOne({_id: this.params._id});
        }
    });

当前,它会创建一个href,如:

Currently, it creates an href like :

 post/ObjectID("52e16453431fc2fba4b6d6a8")

点击它会打开一个网址

post/ObjectID("52e16453431fc2fba4b6d6a8") 

但是,我得到的是"NotFound"模板而不是帖子.

However, I get the "NotFound" template instead of the post.

我该如何解决?

您需要更改pathFor 'post'以传递ObjectId 52e16453431fc2fba4b6d6a8而不是ObjectId('52e16453431fc2fba4b6d6a8')

You need to change the pathFor 'post' to pass the hex representation of the ObjectId 52e16453431fc2fba4b6d6a8 instead of ObjectId('52e16453431fc2fba4b6d6a8')

尝试这样的方式pathFor 'post' _id=this._id.toHexString

一旦您传递了十六进制字符串,就可以在路由器中使用它

Once you are passing the hex string, you can use this in your router

return Posts.findOne({ _id: new Meteor.Collection.ObjectID(this.params._id)});