2016-02-05 55 views
-1

我的_id值来自铁路由器。如何使用流星在mongo collection.findOne()中传递动态_id值?

我的代码:

Template.template_name.helpers({ 
    'edit': function() { 
     var id = Router.current().params._id; 
     return Collection.find.One({ _id: id}); 
    } 
}); 

上面的代码不起作用。

+1

它是如何不工作?请解释您所面对的行为,并告诉我们您是否有任何错误。看看什么是[mcve]。 –

+0

您在'Collection.find.One({_id:id})'的代码中存在拼写错误。您需要将其重写为'Collection.findOne({_id:id})' – chridam

+0

@Kyll var id正在获取值,但Collection.findOne({_ id:id})没有考虑id值并且给出了未定义的值。如何解决它? –

回答

0

首先检查ID是否正确返回或不带有警报或控制台。

Template.template_name.helpers({ 
'edit': function() { 
    var id = Router.current().params._id; 
    alert(id); 
    return Collection.find({ _id: id}); 
    } 
}); 

而且在你的路由器通过这样的:

Router.route('/template/:_id', { 
name: 'template', 
template: 'template', 
}); 
+0

是路由器正在返回id和id也获得正确的值,并且一切都很好与路由器,唯一的问题是Collection.find()不接受id值,所以这需要被修复。如果我传递一个静态值而不是id然后它可以工作,但我需要发送动态值。 –

+0

你能告诉我id的类型是指对象还是字符串 – sravanthi

+0

请检出id值和数据库值两种数据类型是否相等 – sravanthi