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