add new data to json with angular js - angularjs

Hello I am using angular js. Ii try to delete data from my json but when I refresh the page the json file still didn't change. The data I have deleted still there.
I put my json on myjson.com
[
{
"id":1,
"name": "sarah",
]
},
{
"id":2,
"name": "tommy"
]
}
{
"id":2,
"name": "rian"
]
}
]
This is my code for delete the data:
$scope.deleteItem =function(experience){
var index = $scope.experiences.indexOf(experience);
alert("Deleting DATA: "+ index);
$scope.experiences.splice(index,1);
};
Do I need to code php or something? I thought I can delete the file with just code from angularjs. I'm still new here so please can you tell me.

Related

Finding image uri in a nested array

I am trying to get the image uri from this payload. I can get to the image array with this but no further.
bikesArray.map(bikes => bikes.customerBikeImage.assets)
The entire payload is like this.
{
"customerBikeComponents": "SRAM",
"customerBikeCondition": "Used",
"customerBikeImage": {
"assets": [
Array
]
},
"customerBikeMake": "Planet C",
"customerBikePrice": "1500",
"customerBikeRegister": "Yes",
"customerBikeSize": "80"
},
Then the array within the payload is:
[
{
"fileName": "BA459947-3C2A-49CE-8E44-D74B92D5EA49.jpg",
"fileSize": 3583973,
"height": 3088,
"type": "image/jpg",
"uri": "file:///var/mobile/Containers/Data/Application/DF4C4FB4-191F-4101-9A11-4FD728B3CE4B/tmp/BA459947-3C2A-49CE-8E44-D74B92D5EA49.jpg",
"width": 2316
}
],
I am trying to get to the uri and assign that to a state.
i have tried several different ways to no avail and thought I would get some help.
hope this will help,
if you have mutiple imageUri
and you want your end result looking like that:
[
[www.sampleImage1.jpg,www.sampleImage2.jpg],
[www.sample2Image1.jpg,www.sample2Image2.jpg]
]
you need to map on the nested array as well.
bikesArray.map(
bike =>{
let imageUri;
/** if there are multiple uri */
imageUri= bike.customerBikeImage.assets.map(fileData=>fileData.uri)
return imageUri
}
)
if you have only one imageUri in the array
and you want your end result looking like that:
[
www.sampleImage1,
www.sample2Image1
]
just take the first element.
bikesArray.map(
bike =>{
let imageUri;
/*if there is only one uri */
imageUri= bike.customerBikeImage.assets[0].uri
return imageUri
}
)
You are trying to access Array when your data is Object.
const dataDB = {
customerBikeComponents: "SRAM",
customerBikeCondition: "Used",
customerBikeImage: {
assets: [
[
{
fileName: "BA459947-3C2A-49CE-8E44-D74B92D5EA49.jpg",
fileSize: 3583973,
height: 3088,
type: "image/jpg",
uri:
"file:///var/mobile/Containers/Data/Application/DF4C4FB4-191F-4101-9A11-4FD728B3CE4B/tmp/BA459947-3C2A-49CE-8E44-D74B92D5EA49.jpg",
width: 2316
}
]
]
},
customerBikeMake: "Planet C",
customerBikePrice: "1500",
customerBikeRegister: "Yes",
customerBikeSize: "80"
};
Try to use property accessors and pick first item of assets Array using [0] The full route:
data.customerBikeImage.assets[0][0].uri
Also check this codesandbox react example where assets array has object child.

mongo update : upsert an element in array

I would like to upsert an element in an array, based on doc _id and element _id. Currently it works only if the element is allready in the array (update works, insert not).
So, these collection:
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": [
{
"_id": "5a65fd0363e2a32531ed9f9c"
}
]
}]
Receiving this request:
query = { _id: '5a65fcf363e2a32531ed9f9b', 'ressources._id': '5a65fd0363e2a32531ed9f9c' };
update = { '$set': { 'ressources.$': { '_id': '5a65fd0363e2a32531ed9f9c', qt: '153', unit: 'kg' } } };
options = {upsert:true};
collection.update(query,update,options);
Will give this ok result:
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": [
{
"_id": "5a65fd0363e2a32531ed9f9c",
"qt": 153,
"unit": "kg"
}
]
}]
How to make the same request work with these initial collections:
[{
"_id": "5a65fcf363e2a32531ed9f9b"
}]
OR
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": []
}]
How to make the upsert work?
Does upsert works with entire document only?
Currently, I face this error:
The positional operator did not find the match needed from the query.
Thanks
I also tried to figure out how to do it. I found only one way:
fetch model by id
update array manually (via javascript)
save the model
Sad to know that in 2018 you still have to do the stuff like it.
UPDATE:
This will update particular element in viewData array
db.collection.update({
"_id": args._id,
"viewData._id": widgetId
},
{
$set: {
"viewData.$.widgetData": widgetDoc.widgetData
}
})
$push command will add new items

Angular $http service not getting objects from .JSON file

Here is my http service code:
app.controller('StoreController', ['$http', function($http){
var store = this;
store.products = [];
$http.get('/store-products.json').then(function(data){
store.products = data;
});
}]);
And here is my JSON code:
[
{
"name": "...",
"price": 20.00,
"description": "...",
"canPurchase": false,
"images": [
"...jpg",
"...jpg",
"...jpg"
],
"reviews": []
},
{
"name": "...",
"price": 15.95,
"description": "...",
"canPurchase": true,
"images": [],
"reviews": []
}
]
When I run the code on localhost server, it does not show my objects. There is also no errors in the console that show so I cannot see where I am going wrong. Can anyone see the issue here?
your json data is wrapped at response.data of $http.get.
change to below code will solve the problem(also make sure your json file is at right location).
$http.get('/store-products.json').then(function(res){
store.products = res.data;
});
Plunker demo.

How do I get and set model data that's nested deep in Backbone?

I have this model and I want to update the team value when I gather the correct info on a form submit but I'm not sure how to the specific team. I know in backbone I can use the model to get teams but I'm not sure how to then go from teams into the correct team using the code attribute?
I've tried:
// My new team to replace team in code EFU"
var newTeam: {
"id": "POS-876",
"name: "Swansea City"
}
var teams = this.model.get('teams');
var selectedTeam = teams.get('EFU'); // I know this is wrong
selectedTeam.set('team', newTeam);
I'm not sure how I target the correct team then update the team details?
Model
{
"id": "4V6",
"name": "Premier League",
"teams": [{
"code": "EFU",
"team": {
"id": "POS-1",
"name": "Manchester United"
}
}, {
"code": "BMD",
"team": {
"id": "POS-223",
"name": "Chelsea"
}
}]
}
Your Model is a bit confusing. Why is it wrapped in array brackets? And why are you getting 'exchanges' instead of 'teams'? And should perhaps teams be a collection instead of a simple array?
But in any case, to find the specific team, you can use Underscore.
var selectedTeam = _.findWhere(teams, {code: 'EFU'});

Backbone Model.fetch returns data but does not update model

I am running into an issue when my Model is fetched from the server. I see the correct JSON returned back from the server in chrome dev tools but the model does not update with the returned values.
var listtemplate = new ListTemplateModel.Model({id: id});
listtemplate.fetch();
I see the correct data in Chrome dev tools at this point. Here is what comes back from the server:
{
"title": "Template one",
"id": "template_one",
"steps": [
{
"description": "I love it",
"id": 1,
"created_at": "2012-12-24T18:01:48.402Z"
},
{
"description": "This is rubbish!",
"id": 1,
"created_at": "2012-12-24T18:01:48.402Z"
}
],
"created_at": "2012-12-24T18:01:48.402Z"
}
but console logging the JSON show me just the default value and the id that was passed in during the model creation.
console.log(listtemplate.toJSON());
and this returns:
{id: "template_one", title: "", steps: Array[0]}
My model looks like this (I am using Require.js, hence the Model has been renamed to ListTemplateModel above)
var Model = B.Model.extend({
defaults: {
title: '',
id: 0,
steps: []
},
urlRoot: 'xxx'
});
Any ideas?
Edit
#Amulya's answer set me on the right track and then i discovered "then". Hope this helps someone running into the same issue:
listtemplate.fetch().then(function(){
//update the view
});
The reason maybe because you do not wait for the fetch to be completed. Try this:
var listtemplate = new ListTemplateModel.Model({id: id});
listtemplate.fetch({
success: function() {
// fetch successfully completed
console.log(listtemplate.toJSON());
},
error: function() {
console.log('Failed to fetch!');
}
});

Resources