简体   繁体   中英

Read column from Datastore Amplify using react

I am writing a react app using AWS amplify datastore library, I want to read a whole column and put it in the drop-down select menu. I have completed designing the UI but I don't how to get only one column rather than having the whole table Currently my query statement looks something like this await Datastore.query(myTable); This returns the whole table. I want to know if I can get myTable.id where 'id' is column name

Because Amplify Datastore is already maintaining a local replica of your data, the recommendation from Samuel above is reasonable. Writing something like const ids = await Datastore.query(myTable).map(record => record.id) to get the id fields for all records in your table should work, and unless your data is really massive (in which case I'd imagine you're not trying to do that to fill a dropdown in UI) this should be reasonably fast.

The other alternative would be to query using the API category https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/js/ with a custom graphql doc that included only id in the selection set, but that's not really recommended, since you're going to need to hit your backend API unnecessarily in that case, rather than relying on the local data which Datastore maintains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM