0

I'm sorry if I have some stupid question, in my end, I'm a newbie in node.js, my probme is i'm failed to displaying data from entity in express.js framework, I'm using Mysql with TypeORM, and ejs template engine.

I have tried many examples, and below is one of them https://github.com/typeorm/typescript-express-example but seem's not work.

my editor is VSC, and below is my directory. directory

below is my codes

1 - the entity (MasterKategori.ts):

import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";

@Entity("mst_kategori")
export class MasterKategori {

    @PrimaryGeneratedColumn()
    public id: number;

    @Column("varchar")
    public KodeKategori: string;

    @Column("varchar")
    public NamaKategori: string;

    @Column("varchar")
    public Keterangan: string;

    @Column("timestamp")
    public AddDate: Date;

    @Column("datetime")
    public ModifiedDate: Date;

    @Column("int")
    public ModifiedUser: number;

    @Column("int")
    public AddUser: number;
}

2 - index.ts

below is my index.ts, in this script I called the routes (routes.ts):

import dotenv from "dotenv";
import express from "express";
import path from "path";
import "reflect-metadata";
import {createConnection} from "typeorm";

import * as routes from "./routes";
createConnection().then(async (connection) => {
    dotenv.config();

    const port = process.env.SERVER_PORT;
    const app = express();

    app.set( "views", path.join( __dirname, "views" ) );
    app.set( "view engine", "ejs" );

    routes.web(app);

    app.listen(port);
    console.log(`Alhamdulillah, aplikasi ini menggunakan port ${port}!`);
}).catch((error) => console.log("TypeORM connection error: ", error));

3 - routes.ts:

import * as express from "express";
import HomeController from "./controllers/HomeController";

export const web = ( app: express.Application ) => {
   // default homepage routes, and calling the controller
   app.get( "/", HomeController.home);
};

4 - mycontroller (HomeController.ts), in this code I called the entity (MasterKategori.ts) and rendered to the view, I'm using ejs template engine

import * as express from "express";
import {getConnection} from "typeorm"; //calling typeORM
import {MasterKategori} from "../entity/MasterKategori"; //calling the Entity

class HomeController {
    public home = (request: express.Request, response: express.Response) => {
        const kategoriData = getConnection().getRepository(MasterKategori).find(); //get all data
        const kategori = Object.entries(kategoriData);
        response.render("page", {kat: kategori});
    }
}

export default new HomeController();

5 - the view (page.ejs)

<ul>
    <% kat.forEach(function(katdata){ %>
        <li><%= katdata.NamaKategori %> - <%= katdata.KodeKategori %></li>
    <% }) %>
</ul>

and below is the mysql table and records:

select * from `mst_kategori`;

    id  KodeKategori  NamaKategori  Keterangan  AddDate              ModifiedDate         ModifiedUser  AddUser  
------  ------------  ------------  ----------  -------------------  -------------------  ------------  ---------
     1  Coba          Coba          Coba        2019-05-25 09:21:00  0000-00-00 00:00:00             0          0
     2  lagi          lagi          lagi        2019-05-25 10:02:48  0000-00-00 00:00:00             0          0

Unfortunately the result is given blank in the browser, and if I do 'console.log(kategori)' the return is '[]' (no data given).

I'm really appreciate for your any kind of help, I' getting headache for this problem in my end.

however I'm very newbie in node.js, this is will help me to start with ORM and MVC in express.js for my project

Community
  • 1
  • 1
blurify
  • 11
  • 2
  • I think getConnection().getRepository(MasterKategori).find() is an asynchronous function, so trying to access kategoriData before it ends might give an empty response. Try implementing async/await for getConnection().getRepository(MasterKategori). Hope it helps you. – Bharathkumar kamal May 26 '19 at 14:37
  • thanks for your explanation, how to put an async in the class such a Controller class? – blurify May 26 '19 at 14:40
  • https://stackoverflow.com/a/42964310/10499910 This might give you an idea of how to create async function – Bharathkumar kamal May 26 '19 at 14:46
  • Again thank you for the update, use async I can't make it with view 'render', I'll be grateful if you have other clue, thanks – blurify May 26 '19 at 16:08
  • Can you update the question with code changes made?? – Bharathkumar kamal May 26 '19 at 16:12

2 Answers2

1

it works Now, I put 'await', thank you very much, below is the update:

import * as express from "express";
import {getConnection} from "typeorm";
import {MasterKategori} from "../entity/MasterKategori";

class HomeController {

    public home  = async (request: express.Request, response: express.Response) => {
        const kategoriData = await getConnection().getRepository(MasterKategori).find();
        const kategori = Object.entries(kategoriData);
        console.log(kategori);
        response.render("page", {kat: kategori});
    }
}

export default new HomeController();
blurify
  • 11
  • 2
0

Sorry the error is

HomeController.ts:7:26 - non-arrow functions are forbidden

below is the changes

import * as express from "express";
import {getConnection} from "typeorm";
import {MasterKategori} from "../entity/MasterKategori";

class HomeController {

    public home  = async function(request: express.Request, response: express.Response) {
        const kategoriData = getConnection().getRepository(MasterKategori).find();
        const kategori = Object.entries(kategoriData);
        console.log(kategori);
        response.render("page", {kat: kategori});
    };
}

export default new HomeController();

blurify
  • 11
  • 2
  • Also I have changes to: class HomeController { public home = async (request: express.Request, response: express.Response) => { const kategoriData = getConnection().getRepository(MasterKategori).find(); const kategori = Object.entries(kategoriData); console.log(kategori); response.render("page", {kat: kategori}); } } but still the data is not show – blurify May 26 '19 at 17:08