0

I'm creating a programmer job board app and I'm trying to display json data on my main page. At some point I'll render it, but for now I'm just trying to get it to show up in json form so that I know it works.

I'm able to connect to the server, but when I load the page I get a TypeError (Job.showAllJobs is not a function).

I'm using a crud app I made the other week as a reference, but there are a few differences between it and this project that are throwing me off.

Here's my project's file structure:

job-board

  • database

    • connection.js
    • schema.sql
  • models

    • Job.js
    • User.js
  • views

    • index.ejs
    • login.ejs
  • server.js

Unlike my previous crud app, this project is using a connection.js file that gave some trouble earlier. At first I thought I was out of the woods, but I think it might be responsible for my current problem.

Not getting GET to work might seem like a minor error, but it's really bugging me and I haven't been able to keep working because of it.

I populated my table (jobs) with a sample listing as a test, but in the very near future I plan on connecting the app to the GitHub jobs api.

server.js:

const express = require('express');
const app = express();
const PORT = 3000;
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const Job = require('./models/Job');
const User = require('./models/User');
const connection = require('./database/connection')

app.use(bodyParser.json())

app.use(methodOverride('_method'));

const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.set("view engine", "ejs");

///// GET /////

// GET INDEX
app.get('/', (request, response) => {
  Job.showAllJobs().then(everyJob => {
    response.json('index');
    // response.render('index', { jobs: everyJob });
  });
});

Job.js

const Job = {};

const db = require('../database/connection');

///// JOBS /////


/// INDEX ///
Job.showAllJobs = () => {
  return db.any('SELECT * FROM jobs');
};

module.exports = Job;
module.exports = db;

connection.js

// require database setup to use pg-Promise
const pgp = require('pg-promise')({});

// connection url
const connectionURL = "postgres://localhost:5432/job_board";

// new database connection
const db = pgp(connectionURL);

// module.exports = db;
Tsardines
  • 883
  • 2
  • 8
  • 17

1 Answers1

1

You have a couple of problems here.

  • Make sure you're passing the jobs into res.json instead of the string 'index'
  • Make sure you're exporting db from connection.js
  • You're exporting both Job and db from Job.js. Since you're exporting db second, it's overriding the export of Job.
JSilv
  • 1,035
  • 12
  • 26