Loopback4使用参数/ER_BAD_FIELD_ERROR执行sql:未知列'reactionType'在“字段列表”中

发布于 2025-01-09 23:32:33 字数 752 浏览 3 评论 0原文

我想根据从 api 获取的“reactionType”值将表中“reactionType”列的值增加 1。反应类型是一个可变端点。例如/喜欢/不喜欢... 如果收到点赞,我会增加点赞栏的值。如果有不喜欢,我会增加不喜欢列的值。

@patch('/news-reaction/{id}/{reactionType}')
  @response(204, {
    description: 'updateReaction PATCH success',
  })
  async updateReaction(
    @param.path.number('id') id: typeof  News.prototype.id,
    @param.path.string('reactionType') reactionType: string,
  ): Promise<void> {
    await this.newsReactionRepository.execute('UPDATE `news_reaction` SET `${reactionType}` =  `${reactionType}` +1 WHERE `newsId`=?', [id,reactionType]);
  }
}

我收到错误。应该怎样呢?

错误:请求 PATCH /news-reaction/431224/sad 失败,状态代码为 500。错误:ER_BAD_FIELD_ERROR:“字段列表”中存在未知列“${reactionType}”

I want to increase the value of the 'reactionType' column in my table by 1 according to the 'reactionType' value I get from the api. reactionType is a variable endpoint. e.g /like /dislike ...
If a like is received, I will increase the value of the like column. If there is a dislike, I will increase the value of the dislike column..

@patch('/news-reaction/{id}/{reactionType}')
  @response(204, {
    description: 'updateReaction PATCH success',
  })
  async updateReaction(
    @param.path.number('id') id: typeof  News.prototype.id,
    @param.path.string('reactionType') reactionType: string,
  ): Promise<void> {
    await this.newsReactionRepository.execute('UPDATE `news_reaction` SET `${reactionType}` =  `${reactionType}` +1 WHERE `newsId`=?', [id,reactionType]);
  }
}

I'm getting an error. how should it be?

error: Request PATCH /news-reaction/431224/sad failed with status code 500. Error: ER_BAD_FIELD_ERROR: Unknown column '${reactionType}' in 'field list'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-16 23:32:33

我认为您的代码中有两个问题:

  1. ${} 替换在这里不起作用
  2. 参数替换与 ? 是一对一的。为了让它们出现,

我无法测试它,但希望这应该有效

await this.newsReactionRepository.execute(
  'UPDATE `news_reaction` SET `?` = `?` + 1 WHERE `newsId` = ?', [reactionType,reactionType,id]
);

I think there are two issues in your code:

  1. ${} replacements do not work here
  2. parameter replacements are 1-to-1 with ? in order they appear

I couldn't test it but hope this should work

await this.newsReactionRepository.execute(
  'UPDATE `news_reaction` SET `?` = `?` + 1 WHERE `newsId` = ?', [reactionType,reactionType,id]
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文