0

I'm working on a simple To Do app (using pg-promise, embedded js, node.js, and regular JS) and I'm having trouble getting the create functionality to work. I'm able to view every post, view only one post, and delete posts (although this part can only be done in Postman).

When I try to run it, I get the error message: "UnhandledPromiseRejectionWarning: error: invalid input syntax for integer: "NaN"

I'm also getting told that data in the server.js file isn't defined. I pinpointed the line below.

Finally, I'm not super familiar with promises. Thought I should mention that.

Here's a basic overview of the file structure:

1. models
   - task.js
2. views
   - edit.ejs
   - index.ejs
   - new.ejs
   - show.ejs
3. server.ejs, package.json, form.html

Database name: todo_app

Table name: tasks

id | subject  |             content
----+----------+---------------------------------
  1 | planning | plot world domination
  2 | garden   | find victims for venus fly trap
  3 | food     | buy pickles
  4 | postman  | test post
NOTE: Number 4 I added with Postman.

server.js

const express = require('express');
const app = express();
const PORT = 3000;
const bodyParser = require('body-parser');
const task = require('./models/task')

app.use(bodyParser.json())

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

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

const createTask = require('./models/task').createTask;
const editTask = require('./models/task').editTask;
const deleteTask = require('./models/task').deleteTask;

app.get('/new', (request, response) => {
  response.render('new');
})

The following bit of code is where I'm getting told that 'data' is undefined:


// POST
app.post('/new', urlencodedParser, (request, response) => {
  const taskNew = request.body;
  createTask(data).then(taskNew => {
    response.render('new', { tasks: taskNew })
  })
  .catch((error) => {
    response.send(error);
  })
});

app.listen(PORT, () => {
  console.log(`Welcome to the Year ${PORT}, the world of tomorrow!`)
});

task.js

const pgp = require('pg-promise')({});

const connectionURL = "postgres://localhost:5432/todo_app";

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

const createTask = data => {
  return db.one('INSERT INTO tasks(subject, content) VALUES($[subject], $[content]) RETURNING id', data)
};

module.exports = {
  findAllTasks: findAllTasks,
  findById: findById,
  createTask: createTask,
  editTask: editTask,
  deleteTask: deleteTask
};
Tsardines
  • 883
  • 2
  • 8
  • 17
  • 1
    data is undefined, you probably want `taskNew` – George Mar 19 '18 at 02:48
  • `I get the error message` - any indication which part of your code is causing this? – Jaromanda X Mar 19 '18 at 02:48
  • @GeorgeCampbell I changed taskNew and it's working in Postman, but I'm still getting the promise rejection warning. :| – Tsardines Mar 19 '18 at 02:50
  • `const taskNew = request.body; createTask(data).then(taskNew => {` - as earlier comment suggested, you probably want to use `createTask(data).` ... or even `const data = request.body` – Jaromanda X Mar 19 '18 at 02:50
  • the only Promise obvious in your code has a `.catch`, so that's not the unhandled rejection - possibly code you haven't posted is the problem – Jaromanda X Mar 19 '18 at 02:52
  • `db.one(...)` looks like a function, you are returning a function from `createTask` - it looks wrong. Maybe you need a .then().catch() instead? – George Mar 19 '18 at 02:53
  • `db.one(...).then(...).catch(...)` – George Mar 19 '18 at 02:57

0 Answers0