i been tryi'n to use angular nowadays. i'm just hoping if someone could help me why i cant fetch localstorage data like the below codes. thanks
[
{
"name": "firstname",
"email": "email#yahoo.com"
}
]
service.js
getItem: function (item) {
var temp = localStorage.getItem(item);
if (!temp){
return [];
}
return JSON.Parse(temp);
}
controller.js
profile.push({
name: 'firstname',
email: 'rmail#yahoo.com'
});
localStorage.setItem('profiles', JSON.stringify(profile));
console.log(service.getItem('name') + ' : this should output the name');
console.log(service.getItem('email') + ' : this should output the email');
Can you please try this
var array = service.getItem('profiles');
for(var i=0;i<array.length;i++){
console.log(array[i].name, array[i].email)
}
To forego using parsing and deal with any of this you can try a factory I recently created, it let's you store and retrieve arrays and objects. Also it let's you bind to $scope.variables:
https://github.com/agrublev/Angular-localStorage
Related
I have a test in Postman where I do a post request and need to parse the json response
The response looks like this:
"aPIProxy" : [ {
"name" : "SFDC-UpdateLoginTime-v1",
"revision" : [ {
"configuration" : {
"basePath" : "/",
"steps" : [ ]
},
"name" : "1",...some attributes}]
and i need to get something like :
"name" : "SFDC-UpdateLoginTime-v1"
"name" : "1"
for a multiple occurrence json file.
The below postman script might help you.
var jsonData = JSON.parse(responseBody);
var jsonNamesData = jsonData.aPIProxy;
console.log(jsonNamesData);
var parsedData = "";
for(var i=0;i<jsonNamesData.length;i++){
parsedData = parsedData +"\"name\" : \"" +jsonNamesData[i].name+"\", ";
console.log("\"name\" : \"" +jsonNamesData[i].name+"\"");
}
console.log(parsedData);
postman.setEnvironmentVariable("parsedNamesResponse", parsedData); // updating parsed data to the environment variable parsedNamesResponse
You could capture multiple 'name' properties using the _.map() function of Lodash, which is a built it module on the native application. I've had to modify what you need slightly as the name key would have been a duplicate.
const result = _.map(pm.response.json().aPIProxy, data => ({
name: data.name,
revisionName: data.revision[0].name
}))
pm.environment.set("response", JSON.stringify(result))
This would then store all the values in an environment variable for you to use elsewhere in another request.
You should first parse the response using JSON.parse, then you can iterate on the parsed object like:
var resObj = JSON.parse(pm.response.text())
for(var i=0; i< resObj.length; i++) {
console.log("name: "+ resObj[i].name);
}
I'm retrieving the following structure from Firebase:
"bills" : {
"1" : { // the customer id
"orders" : {
"-KVMs10xKfNdh_vLLj_k" : [ { // auto generated
"products" : [ {
"amount" : 3,
"name" : "Cappuccino",
"price" : 2.6
} ],
"time" : "00:15:14"
} ]
}
}
}
I'm looking for a way to process this with Aurelia. I've written a value converter that allows my repeat.for to loop the object keys of orders, sending each order to an order-details component. The problem is, this doesn't pass the key, which I need for deleting a certain order ("-KVMs10xKfNdh_vLLj_k")
Should I loop over each order and add the key as an attribute myself?
Is there a better/faster way?
This answer might be a little late (sorry OP), but for anyone else looking for a solution you can convert the snapshot to an array that you can iterate in your Aurelia views using a repeat.for, for example.
This is a function that I use in all of my Aurelia + Firebase applications:
export const snapshotToArray = (snapshot) => {
const returnArr = [];
snapshot.forEach((childSnapshot) => {
const item = childSnapshot.val();
item.uid = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
You would use it like this:
firebase.database().ref(`/bills`)
.once('value')
.then((snapshot) => {
const arr = snapshotToArray(snapshot);
});
I've a JSON file with a structure that is not yet set; it may grow complex.
I want to keep track of what the functions think the data structure is.
What are standard/smart ways to show that your functions is connected to a certain data structure?
Right now, I'm using a _comment in the json file to keep a version number, and then keeping that version name as a comment in each function that uses it. Full example below.
travel.json
{
"_comment" : "version 1.0"
, "name" : "Tom Sawyer"
, "travel" : [{
"id" : "1"
, "location" : "San Francisco"
}, {
"id" : "2"
, "location" : "London"
}]
}
Two functions to parse: travel.json
fs = require('fs');
function get_json() {
var file = __dirname + '/travel.json';
data = fs.readFileSync(file, 'utf8');
var json_obj = JSON.parse(data);
return(json_obj);
};
function get_location(json_obj) {
// "version 1.0"
var new_obj = {};
json_obj.content.forEach(function(item) {
new_obj[item.id] = item.location;
});
return ( new_obj );
};
// Run
console.log('Locations: ', get_location( get_json() ));
Thanks.
It looks a bit like defensive programming. If you are creating an API that other people will depend on you could version the url of the API instead of the JSON. Something like yoururl/api/1/locations, yoururl/api/2/locations, etc
I'm newbie to Angular Js.I would like to Create Complex Json Object for my Xml settings Like
string Jsonobject=" {'Email': { 'UserName': 'a','Password': 'a'}}";
So far I tried
$scope.save = function () {
var AddSettings = $resource('../AddSettings/');--Calling my WCF REST SERVICE
var adminSettings = new AddSettings();
adminSettings.Title = $scope.inputtitle;
adminSettings.HomeUrl = $scope.intputhomeurl;
//var arr = [];
//arr.push(addmodule.Title);
//arr.push(addmodule.HomeUrl);
//addmodule.LogoSettings = arr;
adminSettings.$save();
It's generating following Json Object : { "UserName": "a","Password": "a"},Can any one please guide me to create a json Object like " {'Email': { 'UserName': 'a','Password': 'a'}}" using Angular js.
Thanks in advance
I solved my problem in the following way.
var adminSettings = new AddSettings();
adminSettings.LogoSettings = {
Title: $scope.inputtitle,
HomeUrl: $scope.intputhomeurl,
logo: $("#ImageUpload > img").attr('src')
};
adminSettings.$save();
It's returning Json object as {'Email': { 'UserName': 'a','Password': 'a'}}.
i saw below example in lawnchair documentation,
var store = new lawnchair({name:'testing'}, function(store) {
// create an object
var me = {key:'brian'};
// save it
store.save(me);
// access it later... yes even after a page refresh!
store.get('brian', function(me) {
console.log(me);
});
});
i am not sure i understood it correctly or not, but based on my understanding, i wrote code like this, (name,dtime,address are variables with value)
db = Lawnchair({
name : 'db'
}, function(store) {
console.log('storage open');
var formDetails = {
"candidateName" : name,
"DateTimeOfVerification" : dtime,
"ResidentialAddress" : address
}
store.save({key:"fdetails",value:formDetails});
store.get("fdetails", function(obj) {
alert(obj);
});
});
but, in alert i did not got value, i got "[object Object]".
1) how to store multi-attribute json object in lawnchair
2) how to get that json object.
Try this:
db = Lawnchair({name : 'db'}, function(store) {
console.log('storage open');
var formDetails = {
"candidateName" : "Viswa",
"DateTimeOfVerification" : "30/07/2012",
"ResidentialAddress" : "3 The Road, Etcc...."
}
store.save({key:"fdetails", value:formDetails});
store.get("fdetails", function(obj) {
console.log(obj);
alert(obj.value.candidateName);
alert(obj.value.DateTimeOfVerification);
alert(obj.value.ResidentialAddress)
});
});
1) You are storing the formDetails structure correctly.
2) obj.value is the collection you are looking for
Had you added the console.log(obj); line into your code and then inspected the console you could probably have worked this out for yourself.