简体   繁体   English

Flow Router不支持ObjectID。 任何修复?

[英]Flow Router doesn't work with ObjectID. Any fix?

I'm trying to build routes in my Meteor app. 我正在尝试在我的Meteor应用程序中构建路线。 Routing works perfectly fine but getting information from db with route path just doesn't work. 路由工作完全正常,但从路径路径获取db的信息不起作用。 I create my page specific routes with this: 我用这个创建我的页面特定路线:

FlowRouter.route('/level/: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: 在我的模板助手中,我得到了我的页面ID:

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

This gets the ObjectID() but in string format. 这将获得ObjectID()但是以字符串格式。 So I try to find that ObjectID() document in the collection with this: 所以我尝试在集合中找到ObjectID()文档:

Levels.findOne({_id: id});

But of course documents doesn't have ObjectIDs in string format (otherwise we wouldn't call it "object"id). 但是当然文档没有字符串格式的ObjectID(否则我们不会将其称为“对象”id)。 Hence, it brings an undefined error. 因此,它带来了未定义的错误。 I don't want to deal with creating my own _id s so is there anything I can do about this? 我不想处理创建我自己的_id s所以我能做些什么吗?

PS: Mongo used to create _id s with plain text. PS:Mongo用于创建带有纯文本的_id Someting like I would get with _id._str now but all of a sudden, it generates ObjectID() . 就像我现在用_id._str得到的一样,但突然之间,它产生了ObjectID() I don't know why, any ideas? 我不知道为什么,有什么想法吗?

MongoDB used ObjectIds as _id s by default and Meteor explicitly sets GUID strings by default. MongoDB默认使用ObjectIds作为_id ,默认情况下Meteor显式设置GUID字符串。

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 ObjectId s being created. 也许您过去使用meteor shell会话插入,现在使用mongo shell / GUI或meteor mongo提示符这样做,这导致创建了ObjectId

If this happens in a development environment, you could generate the data again. 如果在开发环境中发生这种情况,您可以再次生成数据。

Otherwise, you could try to generate new _id s for your data using Meteor.uuid() . 否则,您可以尝试使用Meteor.uuid()为您的数据生成新的_id

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用作特定集合的默认值,可以将其构造函数的idGeneration选项指定为'MONGO'

If you have the string content of an ObjectId and want to convert it, you can issue 如果您拥有ObjectId的字符串内容并想要转换它,则可以发出

let _id = new Mongo.ObjectID(my23HexCharString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM