I am creating an array in JS
for my application
$scope.items = [];
When I add an item to the array we pull information from a Firebase Reference and put it into the items array.
es.addItem = function(item){
var product = $scope.products[item];
$scope.items.push({
'sku' : product.sku,
'name' : product.name,
'pricing' : {
'cost' : product.pricing.cost,
'markup' : product.pricing.markup,
}
});
$scope.newItem = null;
}
Once I am done adding items to the array, I want to be able to save this in Firebase along with the Estimate
information that this is all related to.
es.new = function (reference) {
console.log(angular.toJson($scope.items));
var estimatesRef = firebaseUrl+'estimates';
var estimatesListRef = new Firebase(estimatesRef);
estimatesListRef.push({
'number' : es.number,
'created' : date(),
'expiration' : FDate(es.expiration),
'viewable' : es.viewable,
'client' : es.client,
'author' : authData.uid,
'status' : {
'viewed' : '0',
'approved' : '0',
'denied' : '0',
'expired' : '0'
},
'products' : angular.toJson($scope.items)
});
}
Before I started doing angular.toJson($scope.items)
the client side would give me an error about invalid items in submission.
This all submits but this is how products
are stored in firebase:
"[{\"sku\":\"029300889\",\"name\":\"Test Product\",\"pricing\":{\"cost\":\"10\",\"markup\":\"20\"},\"qty\":\"1\"},{\"sku\":\"4059834509\",\"name\":\"Test Service\",\"pricing\":{\"cost\":\"100\",\"markup\":\"20\"},\"qty\":\"1\"}]"
Clearly not how I am wanting them:
...
{
sku: '',
name: '',
etc....
},
{
sku: '',
name: '',
etc....
},
the $scope.items
array while being ran through angular.toJson()
console logs out like so:
[{"sku":"029300889","name":"Test Product","pricing":{"cost":"10","markup":"20"},"qty":"1"},{"sku":"4059834509","name":"Test Service","pricing":{"cost":"100","markup":"20"},"qty":"1"}]
Which looks alot like what I need minus the encasing []
Is this what is preventing Firebase from saving this correctly? What would your recommendation be?
(I've thought about while I make the Estimate creating a temporary node on my Firebase to save the items, then retrieving those to save the estimate and then deleting the temporary node. That seems like a long task that shouldn't be necessary though. So it's a last resort.)