Store and fetch customObject array in sessionStorage in angular 2 - arrays

I am storing PublicationdtoService[] custom object array in a session storage
_publicationList: PublicationdtoService[] = [];
sessionStorage.setItem('sessionPublicationList',JSON.stringify(this._publicationList));
and fetching it back from the object
this._publicationList =<PublicationdtoService[]> (JSON.parse(sessionStorage.getItem('sessionPublicationList')));
Console.log(this._publicationList);
But i am getting the response in Object[] form and not PublicationdtoService[] form. What do I do?
response in console is
But what I am expecting is:
Please Help

Related

Correct way to get single object forom api reqwest

I have web application based on next js and react js. In this app i fetching data from api using getStaticProps and i return the array with data. But when i try get a single object by id using getServerSideProps i get a whole array of data instede of object that i want tu get.
This is code that i try to get a single object, and in this case i get a whole data:
const response = await fetch(https://api.json-generator.com/templates/someNumder/data?access_token=someToken${context.params.id})
If i place the id variable before access token, then i get 'not found', and in this case i expect to achieve a single object:
const response = await fetch(https://api.json-generator.com/templates/someNumder/data/${context.params.id}?access_token=someToken)

How can i get Json Data if its having in particular Name

Here im having data Like
""{\"resultStatus\":\"success\",\"message\":\"AUTH-040010: Successfully logout user\",\"language\":\"en\",\"region\":\"us\",\"securityToken\":\"C29F32A5-CF46-4CE8-B1DF-4FBC207ABA19|13|1550120060454\",\"userName\":\"John\",\"organisationId\":1,\"isSearch\":false,\"productId\":1,\"productKey\":\"fAyhhy455Hh4d52c\",\"menuKey\":\"LOG\",\"pageDto\":{\"selectedPage\":1,\"totalCount\":0,\"recodsPerPage\":25},\"menuMasterDtoList\":{},\"validated\":true}Redirectdata=https://192.168.10.19:8089/iauth/access/login?productId=1&productKey=f8cc80d…a24d52c&redirectUrl=http://172.16.1.135:458/Enquiry/openenquiries&Message= Success&MessageType=1""
in this Url How can i get Redirectdata Url
JSON.stringify(d.data) By using this i convert that string in Json
But
JSON.stringify(d.data.Redirectdata)
This is Giving me undefind
Please help me how can i get Redirectdata Url
It because Redirectdata is not a JSON key its outside of JSON object

React native fetch... Explain it like I'm 5

export default class App extends Component {
state = {
data: []
};
fetchData = async () => {
const response = await fetch("https://randomuser.me/api?results=5"); // Replace this with the API call to the JSON results of what you need for your app.
const json = await response.json();
this.setState({ data: json.results }); // for the randomuser json result, the format says the data is inside results section of the json.
};
So, I have this code in my App.js file for React Native. The randomuser.me is a website that just gives you random users. Using it as a test URL right now. I don't really understand what the code is doing enough to be able to use it for other parts of my project. I was able to successfully display the 5 user results but now I want to access them again and iterate through the data attribute of the state.
tldr; Can I just access the data I got from the fetch in a for loop using data[i]? Please advise. I want to see if user input matches any of the items in the response that is stored in data attribute of state.
Ok the thign that you just did, that is fetch. You retrieve data from the internet.
"https://randomuser.me/api?results=5" is an API, there is lot of different API's, and each one has it´s own way to retrieve data from. if you put "https://randomuser.me/api?results=5" in your browser, you are gonna see a JSON, some API's store data in JSON, others in an array format.
In this case, the JSON, has just one child, 'results', thats why you store "json.results".
That´s fetch. The thing that you want to do is just javascript.
Store json.results in a variable
then iterate over it
var Results = json.results //store it in a variable
for(var i = 0;i<Object.keys(Results).length;i++){ //iterate
var CurrentUser = Results[Object.keys(Results)[i]] // i use this because some JSOn have random keys
if(CurrentUser.gender==='male'){//if you meet a condition
//do whatever you want
}
}
you can also use ".map" if it´s an array

Fetching data in the form of Array from Mongodb

I am using this function to get the values of all the input tags having name=type1[].
var values = $("input[name='type1[]']").map(function(){
return $(this).val();
}).get();
console.log(values);
When i console.log this value then i get an array in my console window and then i stores this variable inside my mongodb and when i later fetches it then it automatically gets converted into string.
What i wanna do is to get the variable exactly as an array form.
How can i do that?
Here is my Mongodb code and my ajax request
var template = new Temp({
input_text:req.body.input,
template_id:ss.user_id
})
$.ajax({
url:'./save',
type:'POST',
data:'input='+values+'&input1='+values1+'&count='+values1.length,
success:function(response){}
});
And i am using the middleware bodyparser.json() on my server page maybe because thats why it got converted into string.

How to append another cookie object in existing cookie using angularjs?

I want to store new object in existing cookie so that I will have old object as well as new object in cookie.
For example:
$cookieStore.put("myApp", $user); //this is old object
Now, I want to add new object and at the same time also want this old object in my cookie.
//New object
$cookieStore.put("myApp", $Developer);
I want both objects in my cookie.
Could anyone please tell how to do it?
It's Quite simple, you can do this by making array of objects of your cookies object.
var objArr = [];
objArr.push($user);
objArr.push($Developer);
$cookieStore.put("myApp", objArr);
You can make this thing dynamic..
var objArr = [];
function addCookies(obj){
objArr.push(obj);
}
addCookies(YourCookiesObj); //call function by passing your cookies obj ($User, $Developer)

Resources