我正在發佈表單數據。
前端是角度Js。
Iam能夠從前端發佈數據,因爲我登錄角度控制檯,它顯示所有數據。
但是,當沒有節點JS服務器上CONSOLE.LOG,然後我得到陣列領域[目標對象]
非數組字段即對象來了是正確的。
無法將數組數據發佈到使用節點js,angular和mongoDb
型號:
var productsSchema = new Schema({
productName: {type: String, required: false},
productDescription: {type: String, required: false},
productStock: [
{
size: {type: Number, required: false},
price: {type: Number, required: false},
imagePaths: [{
image1:{type: String, required: false},
image2:{type: String, required: false},
image3:{type: String, required: false},
image4:{type: String, required: false}
}]
}
])}
角碼
$scope.product = {};
$scope.newProduct = function(){
var formData = new FormData;
for(key in $scope.product){
formData.append(key, $scope.product[key]);
}
//getting the files
var file = $('#file')[0].files[0];
formData.append('image', file);
//formData.append('product',$scope.product);
//Post data
$http.post('http://localhost:3000/products/api/new-product',formData,{
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then(function(res){
$scope.item = res.data;
});
console.log($scope.product);
//console.log(file.name);
}
爲什麼出現這種情況,我在做什麼錯?
請幫忙。
您是否在發佈之前使用JSON.stringify()數據,而JSON.parse()之後?如果不是,你如何發佈數據,以及如何解析它的服務器端? –
不,我在發佈數據之前不使用JSON.stringify()。我簡單地發佈'$ scope.product'到我的API –
我已經更新了我的角碼在問題中。 –