0

I am currently checking out Vue and am doing a little refactor on a personal project.

I am running into some problems with my API.

The two technologies involved are axios which I am using to send requests to my API, which talks to a postgres database using pg-promise.

The api call...

function add (entry, cb) {
  const length = entry.content.length
  entry.title = `${entry.content.substring(0, 32)}`
  axios.post('/api/notes', entry).then(cb)
}

here, entry is and object { title, content, prio, status, context }

the pg-promise endpoint

export const createNote = (req, res, next) => {
  db.none('insert into entries(title, content, prio, status, context)' +
      'values( ${title}, ${content}, ${prio}, ${status}, ${context})',
    req.body)
  .then(() => {
    res.status(200)
    .json({
      status: 'success',
      message: 'Inserted one entry'
    })
  }).catch(err => next(err))
}

here, req.body is undefined

  1. I don't know why I am getting undefined.
  2. Does this error log help?

I was reading through the documentation at axios and could not seem to find anything wrong with my api call, figured I would post something here.

Thanks!

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Austin Witherow
  • 456
  • 2
  • 4
  • 19
  • 1
    I don't know your issue, but that `if` statement can just be `entry.title = entry.contnt.substring(0, 32);`, you don't need the `if`. – loganfsmyth Oct 11 '16 at 16:43
  • I think `'values( ${title}, ${content}, ${prio}, ${status}, ${context})'` should be a template literal (it should use backticks instead of apostrophes). – Michał Perłakowski Oct 11 '16 at 18:29
  • Check what resulting query you are executing, either by using [pgp.as.format](http://vitaly-t.github.io/pg-promise/formatting.html#.format) or by logging event [query](http://vitaly-t.github.io/pg-promise/global.html#event:query). – vitaly-t Oct 11 '16 at 18:34
  • @loganfsmyth haha :sweat_smile: – Austin Witherow Oct 11 '16 at 19:31
  • @Gothdo I am not sure, it was working this way before with fetch & not axios. – Austin Witherow Oct 11 '16 at 19:31
  • @vitaly-t I run `console.log(pgp.as.format('insert into entries(title, content, prio, status, context) values( ${title}, ${content}, ${prio}, ${status}, ${context})', req.body)) ` and get `insert into entries(title, content, prio, status, context) values( ${title}, ${content}, ${prio}, ${status}, ${context})`. It req.body is not even shown here in this string printout? – Austin Witherow Oct 12 '16 at 09:16
  • @AustinWitherow that doesn't look right. Your output is the original query string, before formatting, but it is supposed to be a formatted query string. Something else is missing there that you do not show in your code here. You can post it on the project's website on GitHub, I will try and help you diagnose what's wrong there. – vitaly-t Oct 12 '16 at 09:33
  • it is kind of a big PR :D the server is build in build & within build I connect the api functions from the server folder. `build/server/api` is where the function calls to the build/server/queries resides. `./src/api/notes` is where the api call resides. https://github.com/awitherow/aether/tree/vue – Austin Witherow Oct 12 '16 at 09:42
  • @vitaly-t i've got it figured out... it was my fault on the side of not having the bodyparser npm module available. – Austin Witherow Oct 12 '16 at 11:38

1 Answers1

2

req.body It has the following structure [{.....}]

for pg-promise need {....}

Solution to the problem req.body[0]

 export const createNote = (req, res, next) => {
  db.none('insert into entries(title, content, prio, status, context)' +
      'values( ${title}, ${content}, ${prio}, ${status}, ${context})',
    req.body[0])
  .then(() => {
    res.status(200)
    .json({
      status: 'success',
      message: 'Inserted one entry'
    })
  }).catch(err => next(err))
}