【问题标题】:error: syntax error at or near "$"错误:“$”处或附近的语法错误
【发布时间】:2017-02-20 06:32:33
【问题描述】:

我目前正在检查 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)
}

这里,条目是 object { title, content, prio, status, context }

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. error log 有帮助吗?

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

谢谢!

【问题讨论】:

  • 我不知道你的问题,但是if 声明可以只是entry.title = entry.contnt.substring(0, 32);,你不需要if
  • 我认为'values( ${title}, ${content}, ${prio}, ${status}, ${context})' 应该是一个模板文字(它应该使用反引号而不是撇号)。
  • 通过使用pgp.as.format 或通过记录事件query 来检查您正在执行的结果查询。
  • @loganfsmyth 哈哈 :sweat_smile:
  • @Gothdo 我不确定,之前使用 fetch 而不是 axios。

标签: javascript ecmascript-6 axios pg-promise


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 2010-12-24
    • 2016-07-10
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多