我使用mongoimport將大量csv文件導入到流星集合中,但是當它們進行插入時,_id值是ObjectID,而流星使用字符串ID。在流星文檔中有一個關於ObjectIDs的小例子,但我並不真正瞭解我應該做什麼。例如,使用鐵路由器我有像這樣如何使用mongoimport的ObjectID?
this.route('profileView', {
path: '/profiles/:_id',
notFoundTemplate: 'notFound',
fastRender: true,
waitOn: function() {
return [Meteor.subscribe('singleProfile', this.params._id, Meteor.userId())];
},
data: function() {
Session.set('currentProfileId', this.params._id);
return Profiles.findOne({
_id: this.params._id
}, {
fields: {
submitted: 0
}
});
}
單一路線,但路線的URL類型的對象,看起來像http://localhost:3000/profiles/ObjectID(%22530845da3621faf06fcb0802%22)
。它也不返回任何內容,頁面呈現空白。這是出版物。
Meteor.publish('singleProfile', function(id, userId) {
return Profiles.find({
_id: id,
userId: userId,
forDel: {
$ne: true
}
});
});
我想我的問題是,我怎麼使用的ObjectID讓路由只使用對象ID的字符串部分,以及如何正確返回的數據?
更新:我已經設法通過將鏈接從<a href="{{pathFor 'profileView'}}" class="profile-details">Details</a>
更改爲<a href="/profiles/{{_id._str}}" class="profile-details">Details</a>
來獲取ObjectID脫離url,所以url現在是http://localhost:3000/profiles/530845da3621faf06fcb0802
。不幸的是,頁面仍呈現空白,我不確定是否因爲我訂閱,發佈或查找收藏品的方式。
如果用'new Meteor.Collection.ObjectID(this.params._id)'替換'this.params._id'的所有實例會發生什麼? – sbking
@Cuberto我得到一個錯誤'從Deps重新計算異常:錯誤:無效的十六進制字符串創建ObjectID' – landland
@landland你只能使用括號中的東西,即'ObjectID(%22530845da3621faf06fcb0802%22)你需要'530845da3621faf06fcb0802'作爲'this.params.id' – Akshat