Lonnie McGill Lonnie McGill - 1 month ago 8
React JSX Question

Getting "TypeError: db.createList is not a function"

I am currently doing a React, Express, Massivejs, postgreSql app. I am getting the error "TypeError: db.createList is not a function" anytime I'm trying to hit my post endpoint. I'm not sure how to remedy it since it looks correct.

My file structure:

enter image description here

My server file looks like this:

var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var massive = require("massive");
var connectionString = 'postgress://LonnieMcGill@localhost/todo';
var massiveInstance = massive.connectSync({connectionString : connectionString})
var config = require('./config.js');


var app = module.exports = express();
app.set('db', massiveInstance);

var listCtrl = require('./controller/listCtrl.js');

// **************** Middleware ****************
app.use(express.static(__dirname + './../public'));
app.use(bodyParser.json());

// ****************** Endpoints ***************
app.post('/api/add/list', listCtrl.createList);
app.get('/api/get/list', listCtrl.createList);


app.get('*', function(req, res) {
res.sendFile('index.html', { root: './public'});
})

app.listen(config.port, function() { console.log('Server initiated on port', config.port); });


My controller looks like this:

var app = require('../server.js');
var db = app.get('db');

module.exports = {

createList: function(req, res, next) {
console.log('db'my);
db.createList([req.body.name], function(err, res) {
res.status(200).send('List created');
})
},

readList: function(req, res) {
db.readList(function(err, res) {
if (err) {
console.log("readList failed");
} else {
console.log("readList working " + req.body, req.params);
}
})
}

}


My createList.sql file looks like this:

INSERT INTO list (
name
)
VALUES (
$1
);

Answer

The documentation clarifies this issue. By default, the "db" folder should stay in the root directory of your project, and not where the scripts consuming the database are (in this case, "server/").

You must either move "db" to the root directory of your project (so as to be alongside "server", "public", etc.), or configure the scripts property to point to that location:

var massiveInstance = massive.connectSync({
  scripts: "server/db",
  connectionString
})
Comments