How to get and iterate questions in ember from database

If you haven’t already done it, the biggest piece of work here is exposing your data on an API endpoint. Same as we discussed in How to connect mySQL and emberJS framework to send and retrieve data.

To load and display the list of questions, the simplest solution is to load the data from your route:

// app/routes/my-page.js
import Route from '@ember/routing/route';
export default class extends Route {
  async model() {
    let response = await fetch('/your-questions-endpoint-here');
    let json = await response.json();
    return json;
  }
}

And then display it in the template like:

{{!-- app/templates/my-page.hbs --}}
{{#each @model as |question|}}
<tr>
  <td>{{question.qid}}</td>
  <td>{{question.questionmessage}}</td>
</tr>
{{/each}}
1 Like