I have few data like below:
Name (string),
Age (string),
Number (string),
ContactList (Array of string),
messages (Array of string),
callHistory (Array of string)
All these above values i have collected from each screen and i saved it in AsyncStorage. I want to send these data as in same format as i mentioned in bracket. But how can i do formated and send to firestore ?.I am new bie. I tried all wasy but not able to understand about doing the formating and sending to fire store.
In my firestore my collection will look like is :
Details (Collection Name) -> UserID(DocumentID) ->Data[]` (In this data i need to save as array. Like each time when i push data to this collection i need to save all in array (index format)).
How can i do that ?.
Any help would be great !
You would do this in 2 steps.
1) Get the data from AsyncStorage[1], store it in a variable in the format you want.
2) Set the variable in firestore.[2]
db.collection("Details").doc("UserID").set(variable).then(function(){
});
[1]https://reactnative.dev/docs/asyncstorage#getitem
[2]https://cloud.google.com/firestore/docs/manage-data/add-data#data_types
Related
I am trying to make a chat app with firebase real time database. I store chats between 2 users as an JSON array in firebase.
Initially I when user used to send message, I used to set whole chat array. But I soon realised that's not a good idea as array will grow.
await firebase.database().ref("chats/" + chatId).set(messages)
When I checked official documentation on how to handle Arrays in firebase, I realised I can use push() instead. I am doing something like this now:
let messageRef = firebase.database().ref("chats/" + chatId)
await messageRef.push().set(message);
It causes 2 problems, one is it generates unique keys and other is when I fetch the chat it returns JSON object instead of Array.
I want something like this:
Instead after using push I am getting:
What's the best way to achieve what I want?
As you can see calling push() will generate a key for you.
Two solutions:
• You let Firebase generate unique key for you and transform the JSON object into an array on client side:
firebase.database().ref("chats/" + chatId).once("value").then(data => {
console.log(Object.values(data.val()));
});
Read more here https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/values
• You use your own keys ("0", "1", "2"...)
firebase.database().ref("chats/" + chatId).child(key).set(msg);
The inconvenience here is you need to know the key. You can use messages.length or store a new field with the size of messages (use transaction to increment it). Don't forget to write rules to prevent users overwriting.
If you can you should use the first solution.
I have JSON data as the response of a query. Then end-user makes two selections from UI and program retrieves the data from the JSON. User's selections are stored in cityNumber and selectParam variables. I want to pass these two variable as the keys. I could pass the cityNumber in [] but selectParam gives error since there is not any key named as selectParam. How can I retrieve the data by passing the selectParam dynamically?
var results = locations[cityNumber].data.selectParam.timeValuePairs;
Use like this
var results = locations[cityNumber]["data"][selectParam][
"timeValuePairs"];
I am using Amazon Web Services database dynamodb. It returns a a JSON which looks liek this:
{"Responses":{"friends":[{"to_username":"u1","from_username":"u2"}]},"UnprocessedKeys":{}}
I need to get the length of the friends array and also get individual values (e.g to_username in the first element of the array which is "u1" in the example).
I have tried something like this:
console.log(data.responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.responses.friends.to_username[0]); //get to_username of the first element in the array
Both return undefined.
Case matters!
console.log(data.Responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.Responses.friends.to_username[0]); //get to_username of the first element in the array
produces the correct results. Note the uppercase R in Responses.
Javascript is a case sensitive language. Please ensure the case in your code matches with the case in your response.
I have some troubles trying to put information to a input text, the info is from localstorage:
key=client,value:
[{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]
and my question is how can i set the identificacion, firstname, lastname and tel to a inputs type text. Im trying with this jquery
$('#name').val(localStorage.getItem("name"));
but im taking the key, no the values.
Thanks for your help!
this is your localStorage
{client: "[{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]", se:fkey: "65d75836cdfe6220e7d8fd44a52ef14e,1438917914"}
to save it to the localStorage Object you need to make any object into a string first
you can do it using JSON.stringify(object)
to save it to your localStorage use localStorage.setItem('key', 'value')
and when you want to fetch it on the localStorage
you can do it using localStorage.getItem('key')
if your item is a json object converted into string
you can make use JSON.parse(string) to bring back to be a json object
First get the data from the localStorage and store them in a variable. Then convert your variable from string to valid JSON format. And finally access the needed data. Here is an example :
var client = JSON.parse(storage.getItem(YOUR_CLIENT_KEY));
...
$('#name').val(client[0].firstname);
Local storage (and session storage) are key/value stores where both the keys and values are strings.
So, when pulling data out of localStorage, it will be a string. In your case, you've stored a JSON object in storage, so to use this as an object, you need to get the string out of localStorage and then use JSON.parse to parse the string and convert it to an object, then you can use the object's properties with your jQuery code:
var data = localStorage.getItem("client");
if(data) {
var obj = JSON.parse(data);
$('#name').val(obj[0].firstName + " " + obj[0].lastName);
}
Be aware that JSON.parse() can throw errors if the string is not valid JSON, so you may want to wrap this call in a try/catch block.
You should log localStorage first to see what is stored in it.
It's weird if the value is
[{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]
because only string type can be stored in localStorage. So you may store the array above via:
localStorage.setItem('key', JSON.stringify(array_above));
Do:
var data = JSON.parse(localStorage.getItem('key'));
when you want to retrieve it.
What's more, your code:
$('#name').val(localStorage.getItem('name'));
works fine.
Here is my jsfiddle demo.
I know I can create JSON format of data in javascript
data = JSON.parse(localStorage.something);
data[0] == "some data";
But when i try to delete its not working.
delete data[0];
delete data[0] will only delete that item from the data array, which is NOT the localStorage item (since, as you already know, that item is a string).
You have to save your changes. Also I would suggest using data.shift() as per minitech's comment instead of delete - delete is better for object properties, not array indices.
data = JSON.parse(localStorage.something);
data.shift(); // delete data[0] without breaking sequence
localStorage.something = JSON.stringify(data);
After you do:
data = JSON.parse(localStorage.something);
Then the data variable holds an idependent object which from now is completely detached from localStorage (which stores just a String that need to be parsed anyway, not a 'real' object).
If you change it, you need to manually update the local storage like that:
localStorage.setItem("something", JSON.stringify(data));
As a side note: if you are aiming at older browsers, then make sure you include something like json2.js to make JSON object work everywhere.