How do i use this fetched data response? - reactjs

I Am using react. I have a fetch setup in an useEffect to grab the data from a database from the backend. It pulls the account and sends it back to the front where i can access it from there.
From the File I want to access the user information i have a console log
I want to be able to grab like the firstname, lastname individually and use them in the script. how do i go about that. I did try to console log UserData.user and it gave me this result
However when i went to try to get firstname like userData.user.firstname i was met with an error. Im pretty new to react and pushing myself with things ive never done before to learn and any help would be great
Thank you everyone who does help it means alot
Please note all information in screenshots are fictitious and are just prop data.
EDIT:

This may be because you are trying to read the user information before the api returns the data. so check if the array has data before trying to access it . You can use save navigation to check for undefined or null
UserData.user[0]?.firstname

Based on your screenshot the data you are getting back is an array of user objects. To access it you will need to use:
userData.user[0].firstname
Note that this will access the first user object in the array - you will likely need checks to ensure the data data exists before accessing it, e.g.:
if (userData.user.length > 0) {
// Access data as needed
}

user is an array so you can acces like this
userData.user[0].firstname;
it will be better if you update your endPoint to get an object instead, so you can have access and check easier

Related

how to store likes data in flutter

I'm developing an app in which I need to show come "coupons" I get from the API. I also have a "liked coupons" page where I need to show the ones the user has liked. I'm facing 2 problems here:
1- I don't know how to store likes, should I implement a local database for everything or should I ask our back-end team to save the liked/not liked state on the server?
2- I have a model class for coupons, and I have a coupon_list widget which is a horizontal listview.builder(). the problem is that some coupons are being showed in 2 or 3 different lists and I need them to all turn to liked when user likes an instance from a single list. how can I do that? (I want to do something like working with pointers in c++, passing the ACTUAL variable instead of it's value so it changes globally)
I would like to suggest you to store it in server as well. (Ask to your back-end team to add parameter) So that if user logout or sign in from different device "liked coupons" data will be available in all cases.
And for 2) multiple coupon entry you have to manage it via unique id. Like every coupon has its unique series no / pattern num. So you can put condition on that. i.e. Add "unique_no" to liked_list from all available list of coupon
Solution
Use Shared Preferences! This is something like a database on the device you are currently running. So if the user makes a like you can save that on their device!
To add Shared Preferences to your app look this video
Hope it helps!

Trying to access information within localStorage

I am trying to access information with-in localStorage from one component to another, specifically the name of the user on their profile. I am able to access up to the object holding all of the profile information like name, email, address etc. However when I try to extract just the name I am unsure of how to do so, or if its even possible.
right now im trying...
console.log(localStorage.document)
this gets me the an object holding all the data in localStorage...
{"name":"John Doe","email":"johndoe#aol.com","phone":"0000000000","address1":"123","address2":"123","addresscitystate":"Seattle, WA","addresszip":"00000"}
yet I cant seem to access any further then that. I attempted to just do [0] at the end to get the index but im getting just the letter back for that specific index not the actual object I want.
Any advise is appreciated!
May be you can try this one, For extracting the specific property from localStorage you can do this:
let item = localStorage.getItem("name")
The way to do this is.
First you need to extract the localStorage item
let savedItem = localStorage.getItem("username")
"Username" is the name of the item that is save in you localstorage.
The Complete guide for the localstorage usage is as follow.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Use query string in URL or use multiple URL?

If I want to display many posts in my web application but every post have its own type and I want to display each type in single page so, What's the best method to do that? Is put all all posts in one url and use query string to filter the posts upon the type and display it in the page?
For example : axios.get('/posts?type =sport')
Or I have to put every single type in separate Url
For example: axios.get('/posts/sport')
Also one more question please?
use one reducer to manage every posts or create one reducer for each post type?
you can add a dynamic route to every new type.
Ex:
'/transaction' -> component-1
'/transaction/:type' -> component-any (multiple)
welcome to Stackoverflow!
I can imagine you have a web API of some sort serving a URL /posts. You want to consume that endpoint from your web application, and you are using axios to do that. I can assume you are using JSON to return that data. Correct me if I'm wrong.
Now that the basic information is "clear", what data you serve from the endpoint, and how it is requested from the client is up to you. Do you want to ask the server what types are there first, and then do one AJAX request per type? Ok. Do you want to serve all posts independent of their type? Ok. Do you want to accept POST data in your controller so you can filter the results before returning a response? Ok.
If you are looking for a more specific answer, you must give more details, or specify more. But I hope I could be of help.
Edit: complete answer.
If you want to filter the results, you have to send some additional data in your POST request, in this case, your post type. In axios, this could be done like this:
axios.post('https://example.com/posts', {
type: 'sports'
}).then((data) => {
console.log(data);
});
You can obviously get the "type" value from a select input, other variable, even the current router page. I don't know your exact setup, but you can always come back and ask ;)
THEN, in your API controller you have to get that POST parameter type, and use it to filter the results. Again, I don't know your exact setup, but for MySQL if would be a WHERE statement in your query, or similar.

How to delete certain children of Firestore field array using Flutter

I am trying to make a Flutter and Firebase fitness application that saves runs a user logs. The runs are saved in an array called 'runs' in the Firestore database. Each document in my database represents a different user's runs, so the 'runs' array is a field found within each user's document. I have been trying to delete only a specific child of 'runs' but have had no luck. Does anyone know how to do this?
Here is my code for the delete function:
final CollectionReference runCollection = Firestore.instance.collection('runs')
Future deleteRun(dynamic runToDelete) async {
return await runCollection.document(uid).setData({
'runs': FieldValue.arrayRemove([runToDelete])
});
}
When I run this, I get no errors but nothing happens in the database.
I have also tried FieldValue.delete() but have not been able to isolate a specific index of 'runs' to delete without deleting the entire array.
Here is a picture of my firestore database:
Firestore.instance.collection(collection name).document(your document id).updateData({"array name": FieldValue.arrayRemove([delete object/array])}).than((value){print("delete");}).catchError((error){print(error);});
FieldValue.delete() only works when you know the entire contents of the array item to delete. It does not work with indexes, nor does it work with child values of array items.
What you will have to do instead is read the document, modify the array in memory, then write the modified array back to the document.

CakePHP Parsing data from file and adding to database

I have a file that I wish to parse and add to the database.
The way I wish to do it is this:
The user uploads the file
File is parsed
The users are given the parsed data and selects what they want to keep
They submit
(More detailed explanation follows)
I have a film script file that I wish to parse and enter scenes and their respective characters and props. I have already managed to parse this file into an array using php.
What I want to do now is to
prompt the user with the various scenes and its contents
allow the user to fix / modify the data before submitting it into the database.
The thing is that the script file has more than one scene in it, so I am unsure how to handle the data-entry aspect of this problem.
Are there any "best practices" for these kinds of data entries?
(if I didn't explain something in enough detail, please tell me and I'll update the question).
I would simply parse the file and save it as it is to the database. Something like Script hasMany Scenes. Let them select their script and simply paginate the Scene records and let them delete them.
Is there an urgent need to not save them all after import? Another downside of your approach is that if the browser crashes or the user closes it he has to start over again because you just kept the data in memory but not persistant.
If you want to rely on your approach you'll have to work with the data coming back from the form and simply remove the key from the form data.
Like doing this in a foreach running over the data structure
if ($data['Scene][$key]['delete'] == 1) { unset($data['Scene'][$key]); }

Resources