Really new to node.js and relatively new to PostgreSQL. For some reason this only creates the first table, not the second. I am probably totally doing this wrong. I can't seem to find any code examples that are similar enough to extrapolate an answer from. I want to create two tables. It seems I can only run one query and that's it. No more. It's suggested that my connection ends before the second can be executed. I don't know why. I can't find a single example of doing this in a dozen different Google searches.
var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE preferences(id SERIAL PRIMARY KEY, food VARCHAR(40) not null, preferred BOOLEAN)');
client.query('CREATE TABLE foods(id SERIAL PRIMARY KEY, food VARCHAR(40) not null, type VARCHAR(40) not null, spicy BOOLEAN, spiciness VARCHAR(10)');
query.on('end', function() { client.end(); });
I had doubts that I could just do another client.query and have it all execute at the end. I took this example out of a tutorial but the second table I added. So I'm just playing with it and trying to learn here with this test.
Just to state my ultimate end goal: I wanna ultimately create a whole script for building out the database completely full of all the necessary tables and data.
Error I get no matter what I try:
$ node models/database.js events.js:85 throw er; // Unhandled 'error' event ^ error: syntax error at end of input at Connection.parseE (~/Documents/test01/node_modules/pg/lib/connection.js:534:11) at Connection.parseMessage (~/Documents/test01/node_modules/pg/lib/connection.js:361:17) at Socket. (~/Documents/test01/node_modules/pg/lib/connection.js:105:22) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)
query.on(type, handler)adds an event handler for the passed in type, "end" in this case. When that event occurs for the query you attached it to, the handler fires. Therefore, the handler fires when the first query is complete, which is likely before the second is complete.