Cannot read property 'insert' of undefined
I created a few .sql
files:
createcustomerstable.sql
BEGIN;
DROP TABLE IF EXISTS customer;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS Customer (
ID UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
CompanyName1 VARCHAR(40),
CreatedAt DATE DEFAULT now()
);
END;
seedcustomertable.sql
INSERT INTO public.customer (companyname1) VALUES ('GitHub');
Then I created a little app:
app.js
var express = require("express");
var app = express();
var http = require('http');
var massive = require("massive");
var connectionString = "postgres://postgres:postgres@localhost/postgres";
var bodyParser = require("body-parser");
var massiveInstance = massive.connectSync({connectionString: connectionString});
app.set('db', massiveInstance);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var db = app.get('db');
db.createcustomerstable(function(err, res) {
db.seedcustomertable(function(err, res) {
});
});
app.post('/api/customers', function(request, response) {
var db = app.get('db');
var customer = request.body;
db.customer.insert(customer, function(error, newCustomer) {
response.status(201).json(newCustomer)
})
});
Then I created a test:
test.js
var request = require('supertest');
var app = require('../app.js');
var assert = require("assert");
describe('POST /api/customers/', function () {
it('responds with customers', function (done) {
var customer = { companyname1 : "Microsoft"}
request(app).post('/api/customers/')
.set('Content-type', 'application/json; charset=utf-8')
.send(customer)
.expect(function (res) {
assert.equal(res.body.companyname1,'Microsoft');
})
.end(done)
});
});
Running the test results in:
TypeError: Cannot read property 'insert' of undefined
at /home/alex/src/hellomassive/customerservice/app.js:55:16
at Layer.handle [as handle_request] (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/layer.js:95:5)
at next (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/layer.js:95:5)
at /home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:330:12)
at next (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:271:10)
at urlencodedParser (/home/alex/src/hellomassive/customerservice/node_modules/body-parser/lib/types/urlencoded.js:81:44)
at Layer.handle [as handle_request] (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:312:13)
at /home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:330:12)
at next (/home/alex/src/hellomassive/customerservice/node_modules/express/lib/router/index.js:271:10)
at /home/alex/src/hellomassive/customerservice/node_modules/body-parser/lib/read.js:121:5
at done (/home/alex/src/hellomassive/customerservice/node_modules/body-parser/node_modules/raw-body/index.js:239:14)
at IncomingMessage.onEnd (/home/alex/src/hellomassive/customerservice/node_modules/body-parser/node_modules/raw-body/index.js:285:7)
at IncomingMessage.g (events.js:199:16)
at IncomingMessage.emit (events.js:104:17)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
If I run the test again, it works.
When deleting the table customer
and repeating the steps shown above again, it fails - re-run all is ok.