ChrisM
ChrisM

Reputation: 716

React Firebase - Fetching an item by id

Im trying to implement a details page for a set of projects. On the projects page i have an array filled with projects and showing in a listview. When i clik on a project it goes through to the details page with the :key in the url.

I have the object this.props.params.key which is the identifier for the project but how do i query firebase for that objects details and save it in an array?

Url would be something like

https://website.firebaseio.com/projects/uyrkbqetyqknnu

I basically want to send a GET request to that and grab the details.

Thanks

I have a collection of Items on Firebase

https://i.sstatic.net/kuJem.png

All i really want to do is grab the values and fill some textfields with them.

something like var subject = firebaseobject.item.subject

Upvotes: 1

Views: 1323

Answers (1)

Luke Schlangen
Luke Schlangen

Reputation: 3882

You can create a reference to the specific project data like this

var allProjectsRef = new Firebase('https://website.firebaseio.com/projects');
var projectRef = allProjectsRef.child('PROJECT-ID-GOES-HERE');

I'm guessing you want this to work for more than one project. You can pass this.props.params.key in as a variable like this:

var projectId = this.props.params.key;   
var allProjectsRef = new Firebase('https://website.firebaseio.com/projects');
var projectRef = allProjectsRef.child(projectId);

I'm not sure if this was intentional or just happened to be how you wrote the question, but most of the unique ids in firebase start with a hyphen, so if it's not returning anything, it might be because of that.

The complete documentation is available here: https://www.firebase.com/docs/web/api/firebase/child.html

Upvotes: 1

Related Questions