I recently started using Node-Restful and I am seeing that an array is being returned when I hit a GET request.
What I want
{"_id":"56","name":"something"}
What I am getting
[{"_id":"56","name":"something"}]
How does Node-restful handle GET requests? And how do I override this? Is there anyway that Node-restful takes care of this?
I think that default implementation for get request is to return an array of requested data. if you want just one of them, you should send id of object in database.
GET /resources -> returns array of objects in database
GET /resources/:id -> returns just one object with given id
Related
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
I'm trying to fetch items from GraphQL and receiving (looked at 'Network' tab):
But when I do
console.log(data)
I receive same Object as first a lot of times.
For example, not [0,1,2,3,4,5,6], but [0,0,0,0,0,0].
What's wrong?
Your API is returning null as the value for each item's id property. Apollo's InMemoryCache is normalized and uses the id and __typename of each object is stores as the key. Since the id for each item are the same, they are getting overwritten in the cache.
The easiest solution is to fix your server so it correctly returns the id for each item -- that will fix the unexpected caching behavior. If for some reason you don't have a unique identifier for each item, you'll need to implement your own dataIdFromObject function and pass it in to InMemoryCache's configuration. See here for additional details in the docs.
I am doing full sync this way: list of /threads and then a request to get each of the thread like /threads/{id}. However this returns me every message together with it's body data -> and I just want to fetch the metadata of the messages. I can see that in get 'messages/{id}' you can specify format but not in get threads/{id}
Threads.get() now supports format=METADATA and with that you can use the new "metadataIncludeHeaders" to further limit the headers list to a select few. This is much more efficient than using "fields" as it only fetches what is necessary from the backend rather than filtering it later on:
https://developers.google.com/gmail/api/v1/reference/users/threads/get
I assume that by metadata you mean the headers (no body). You can use the fields parameter to get just that (messages/payload/headers):
https://www.googleapis.com/gmail/v1/users/me/threads/{thread-id}?fields=messages%2Fpayload%2Fheaders&key={YOUR_API_KEY}
I'm using Google AppEngine as backend and AngularJS as front end for web application I'm making. I'm presenting data in pages to the user.
AppEngine has the ability to select data and return 3 pieces of information: the items selected, indication if there are more items and cursor for the next page.
I need to return all 3 pieces to the client app so it can present the fetched items and allow the user to go to the next page.
I also would like to use ngResource to interact with the server.
The problem is that ngResource expect the list of items to be a list and here it is an object with the 3 pieces.
Is there a way to modify ngResource a bit so that after fetching the data it will use the items to build the array of items?
Not necessarily, ngResource can deal with arrays as well as single item or json object. The standard get operation returns a object whereas query returns array. Bottom line as long it is a valid json data ngResource would work.
You can always call get on the resource, get the data into a json object and then it can have sub-properties which can be of array type.
You can share your specific structure and the community can help you with understand how to access it using ngResource
My extjs store is not loaded even if the server reutrns a correct json response.
I checked that the fields return from the response has the same names as the fields defined in the store.
The grid is just blank.
What can I check?
You should check you are returning an array of objects to ExtJS. If you are returning an object, containing the array of objects as a field, make sure you specify rootProperty for JsonReader to find the array. Make sure you are using correct data reader (i.e. JsonReader).
You may also try adding data from response manually into store via add to check that the data fits in well.