2016-11-25 61 views
0

我是新來的燼,作爲第一個應用我試圖建立一個小網上商店。 我可以接收「所有產品」作爲產品概述,但不能通過ID獲得一種特定產品。使用findRecord獲取特定物品

我有以下的router.js:

Router.map(function() { 
    this.route('products'); 
    this.route('product', {path: 'products/:product_id'}); 
}); 

我products.js(至極的作品):

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model(){ 
    return this.get('store').query('product', {}); 
    } 
}); 

而且product.js(至極也會產生問題):

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model(params){ 
    return this.store.findRecord('product', params.product_id); 
    } 
}); 

該項目可根據https://github.com/hatchling-shop/hatchling/tree/master/EmberHatchling

也許你可以幫我一把。 在此先感謝! :)

+0

如果你傳遞一個空對象,你基本上想把查詢換成'findAll',因爲'findRecord'看起來很好,你會得到任何錯誤消息嗎?有網絡請求嗎? –

回答

1

運行代碼後,似乎你在API Product.findById(),而不是在Ember的問題。

在下面的方法:

Product.findById(id, function(id, err, product) { 
    if (err) { 
     res.send(err); 
    } 
    res.json({product: product}); 
}); 

在回調PARAMS是錯誤的,而不是你需要刪除id並更改爲:

Product.findById(id, function(err, product) { 
    if (err) { 
     res.send(err); 
    } 
    res.json({product: product}); 
}); 

希望這有助於。

+0

哇,這解決了我的問題!你是如何得到解決方案的?我搜索了幾乎整整一天。 – LilK3ks