0

我目前正在检查 Vue 并正在对一个个人项目进行一些重构。

我的 API 遇到了一些问题。

涉及的两种技术是axios,我用它来向我的 API 发送请求,它使用pg-promise与 postgres 数据库对话。

api调用...

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

在这里,条目是和对象{标题,内容,优先权,状态,上下文}

pg-promise 端点

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))
}

在这里, req.body 是未定义的

  1. 我不知道为什么我变得不确定。
  2. 这个错误日志有帮助吗?

我正在阅读 axios 上的文档,似乎找不到我的 api 调用有什么问题,我想我会在这里发布一些东西。

谢谢!

4

1 回答 1

2

req.body 它具有以下结构 [{.....}]

对于 pg-promise 需要 {....}

解决问题 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))
}
于 2019-05-25T09:20:08.257 回答