Flow Router不适用于ObjectID.可以解决吗? [英] Flow Router doesn't work with ObjectID. Any fix?

查看:77
本文介绍了Flow Router不适用于ObjectID.可以解决吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Meteor应用中构建路线.路由工作得很好,但是用路由路径从数据库获取信息却行不通.我使用以下方法创建页面专用路线:

I'm trying to build routes in my Meteor app. Routing works perfectly fine but getting information from db with route path just doesn't work. I create my page specific routes with this:

FlowRouter.route('/level/:id'...

这条路线将我带到相关模板,没有任何问题.然后,我想从数据库中获取属于该页面的一些数据.在我的模板助手中,我通过以下方式获取页面的ID:

This route takes me to related template without a problem. Then I want to get some data from database that belong to that page. In my template helpers I get my page's id with this:

var id = FlowRouter.getParam('id');

这将获取ObjectID(),但以字符串格式.因此,我尝试使用以下方法在集合中找到该ObjectID()文档:

This gets the ObjectID() but in string format. So I try to find that ObjectID() document in the collection with this:

Levels.findOne({_id: id});

但是,当然,文档中没有字符串格式的ObjectID(否则,我们将其称为"object" id).因此,它带来了未定义的错误.我不想创建自己的_id,所以我能做些什么吗?

But of course documents doesn't have ObjectIDs in string format (otherwise we wouldn't call it "object"id). Hence, it brings an undefined error. I don't want to deal with creating my own _ids so is there anything I can do about this?

PS:Mongo曾经用纯文本创建_id.就像我现在会使用_id._str一样,但是突然之间,它生成了ObjectID().我不知道为什么,有什么主意吗?

PS: Mongo used to create _ids with plain text. Someting like I would get with _id._str now but all of a sudden, it generates ObjectID(). I don't know why, any ideas?

推荐答案

MongoDB默认使用ObjectIds作为_id,而Meteor默认使用显式设置GUID字符串.

MongoDB used ObjectIds as _ids by default and Meteor explicitly sets GUID strings by default.

也许您以前使用meteor shell会话插入,现在却使用mongo shell/GUI或meteor mongo提示符来执行此操作,从而导致创建了ObjectId.

Perhaps you inserted using a meteor shell session in the past and now used a mongo shell/GUI or a meteor mongo prompt to do so, which resulted in ObjectIds being created.

如果这是在开发环境中发生的,则可以再次生成数据.

If this happens in a development environment, you could generate the data again.

否则,您可以尝试使用Meteor.uuid()为数据生成新的_id.

Otherwise, you could try to generate new _ids for your data using Meteor.uuid().

如果要使用ObjectId作为某个集合的默认值,则可以指定构造函数的idGeneration 选项为'MONGO'.

If you want to use ObjectId as the default for a certain collection, you can specify the idGeneration option to its constructor as 'MONGO'.

如果您具有ObjectId的字符串内容并想要进行转换,则可以发出

If you have the string content of an ObjectId and want to convert it, you can issue

let _id = new Mongo.ObjectID(my23HexCharString);

这篇关于Flow Router不适用于ObjectID.可以解决吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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