了解新的mongo id并将其与iron-router一起使用 [英] understand new mongo id and use it with iron-router

查看:67
本文介绍了了解新的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)});

这篇关于了解新的mongo id并将其与iron-router一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆