How do I get values to render

My code:

const postQuery = await Database.raw(‘SELECT * FROM accounts ORDER BY RANDOM() LIMIT 1’);
const accounts = postQuery.rows

    return view.render('get', { accounts: accounts })

Return the error:
“UNDEFINED”

PLS, HELP ME

Hello @SrEliaas you can make this, following this steps:

First, you need create a controller for render your view:

adonis make:controller Users

Secound, add your controller on your Route file, the file is in start/routes.js

Route.get('/', 'UserController.index');

Third, in your UserController, you need declare the postQuert:

'use strict'
// Include Database
const Database = use('Database')

class UserController {
  async index({ request, view }) {
  const postQuery = await Database.raw('SELECT * FROM accounts ORDER BY RANDOM() LIMIT 1');

    return view.render('user', { postQuery });
  }
}

module.exports = UserController

And in your user view, call the postQuery

{{ postQuery[0].attribute }}

If you are getting undefined, you are likely to have an error with your query in the database

Try to assign any value to the variable and see if the error persists, or try another query.

Regards!

1 Like

Please do not create duplicate issues. Treat the forum and members time with respect

thanks bro

1 Like