2017-10-04 39 views
0

這是從MLAB定義我的產品文檔的摘錄:控制器無法識別對象的屬性

{ 
"_id": { 
    "$oid": "596161e1734d1d25634366ce" 
}, 
"images": { 
    "regular": [ 
    "Amana Electric Range in Stainless Steel-1.jpg", 
    "Amana Electric Range in Stainless Steel-2.jpg", 
    "Amana Electric Range in Stainless Steel-3.jpg", 
    "Amana Electric Range in Stainless Steel-4.jpg", 
    "Amana Electric Range in Stainless Steel-5.jpg" 
    ] 
    } 
} 

這是對應型號的貓鼬的摘錄:

const ProductSchema = new Schema({ 
    images:{} 
}); 

var Product = mongoose.model('Product', ProductSchema, 'product'); 
module.exports=Product; 

我想訪問在我的控制器中的圖像對象,如:

init = function() { 
     $scope.product = DataService.getProduct(); 
     console.log('productController.product = ', $scope.product) 
     $mainImage = $scope.product.images.regular[0]; 
     $productImages = $scope.product.images.regular; 
    }; 

但我得到以下錯誤:

angular.js:14700 TypeError: Cannot read property 'regular' of undefined 
at init (productController.js:8) 
at new <anonymous> (

我在角2類似的問題,並能夠通過定義我的圖片與任何類型的對象在我的模型來解決這個問題:

images: any; 

如何解決這Angularjs?

產生額外的信息:按getProduct(生產

getProduct() { 
    return $filter('filter')(products, {'_id': productId}); 
} 

產品):

[ 
    { 
"_id": "596161e1734d1d25634366ce", 
"producttype": "stove", 
"name": "30 in. 4.8 cu. ft. Electric Range in Stainless Steel", 
"brand": "Amana", 
"model": "AER6303MFS", 
"price": 199.9, 
"list_price": "301.00", 
"description": "This 30 in. Amana Electric Range comes with Bake Assist Temps to help you get dinner going. Simply choose a preset temperature setting to get started. Or, set your bake or broil temperatures with Easy Touch Electronic Controls Plus, which are extra-large and easy-to-read. And when you need to know how your dinner is doing without opening the oven door, the Extra-Large Oven Window lets you see and savor.", 
"rating": 5, 
"sku": "1002064151", 
"warranty": "ANSI Certified,CSA Certified", 
"images": { 
    "stainless": [ 
    "Amana Electric Range in Stainless Steel-1.jpg", 
    "Amana Electric Range in Stainless Steel-2.jpg", 
    "Amana Electric Range in Stainless Steel-3.jpg", 
    "Amana Electric Range in Stainless Steel-4.jpg", 
    "Amana Electric Range in Stainless Steel-5.jpg" 
    ], 
    "white": [ 
    "Amana Electric Range in White-1.jpg", 
    "Amana Electric Range in White-2.jpg", 
    "Amana Electric Range in White-3.jpg", 
    "Amana Electric Range in White-4.jpg" 
    ], 
    "black": [ 
    "Amana-black-1.jpg", 
    "Amana-black-2.jpg", 
    "Amana-black-3.jpg", 
    "Amana-black-4.jpg", 
    "Amana-black-5.jpg" 
    ], 
    "regular": [ 
    "Amana Electric Range in Stainless Steel-1.jpg", 
    "Amana Electric Range in Stainless Steel-2.jpg", 
    "Amana Electric Range in Stainless Steel-3.jpg", 
    "Amana Electric Range in Stainless Steel-4.jpg", 
    "Amana Electric Range in Stainless Steel-5.jpg" 
    ] 
}, 
"specifications": { 
    "dimensions": [ 
    { 
     "value": "45.25", 
     "title": "Depth With Door(s) Open 90 Degrees (In.)" 
    }, 
    { 
     "value": "25", 
     "title": "Oven Interior Depth (in)" 
    }, 
    { 
     "value": "18.38", 
     "title": "Oven Interior Height (in)" 
    }, 
    { 
     "value": "19", 
     "title": "Oven Interior Width (in)" 
    }, 
    { 
     "value": "27.2", 
     "title": "Product Depth (in.)" 
    }, 
    { 
     "value": "46.25", 
     "title": "Product Height (in.)" 
    }, 
    { 
     "value": "29.88", 
     "title": "Product Width (in.)" 
    }, 
    { 
     "value": "30 in.", 
     "title": "Range Size" 
    } 
    ], 
    "details": [ 
    { 
     "value": "Gas Range", 
     "title": "Appliance Type" 
    }, 
    { 
     "value": "Cast Iron", 
     "title": "Burner Grate Material" 
    }, 
    { 
     "value": "9500", 
     "title": "Burner No.1 BTU" 
    }, 
    { 
     "value": "15000", 
     "title": "Burner No.2 BTU" 
    }, 
    { 
     "value": "5000", 
     "title": "Burner No.3 BTU" 
    }, 
    { 
     "value": "15000", 
     "title": "Burner No.4 BTU" 
    }, 
    { 
     "value": "5.1", 
     "title": "Capacity of Oven (cu. ft.)" 
    }, 
    { 
     "value": "Manual Clean", 
     "title": "Cleaning Type" 
    } 
    ] 
}, 

"feature": [ 
    "Large 4.8 cu. ft. oven capacity with space for dinner and dessert", 
    "Versatile cooktop with multiple element options up to 1,800 watts", 
    "Bake Assist Temp presets make cooking even more convenient" 
], 
"$$hashKey": "object:181" 

} ]

+0

什麼是$ scope.product記錄? –

+0

它正在記錄目標產品。 – koque

+0

我明白,但請告訴我們記錄結果,以便我們可以告訴你你在訪問屬性時犯的錯誤。 –

回答

2

你必須指定所需的數組元素的索引,在這種情況下,0

$scope.product[0].images.regular[0]; 
+0

謝謝,豪爾赫。我錯過了。 – koque

0

的問題是,你的DataService.getProduct其承諾,以便 你需要在諾言成功時解決並分享價值。

嘗試更改您的代碼。

init = function() { 
    $scope.product = DataService.getProduct(function(product){ 
     console.log('productController.product = ', product) 
     $mainImage = product.images.regular[0]; 
     $productImages = product.images.regular; 
    }, function(error){ 

    }); //this if is a resource with $resource. 

    $scope.product = DataService.getProduct().then(function(product){ 
     console.log('productController.product = ', product) 
     $mainImage = product.images.regular[0]; 
     $productImages = product.images.regular; 
    }, function(error){ 

    }); // this if is a factory with $http 

}; 

這是異步的常見情況。處理。

UPDATE

的getProduct返回數組,所以你需要先存取權限的陣列。

所以

$scope.product = DataService.getProduct(); 

    $mainImage = $scope.product[0].images.regular[0]; 
    $productImages = $scope.product[0].images.regular; 
+0

這並沒有完全解決問題,耶穌,getProduct()不返回一個承諾。我在原始問題中添加了getProduct()方法。它正在產生預期的數據。 – koque

+0

檢查更新....因爲我didtn知道getProduct返回我猜異步問題....現在我看到它調整了一個數組,而不是像你這樣的對象張貼在問題誰是一個對象。 –