javascript - Knexnest 查询不返回数组中的数据,即使这是预期的

标签 javascript node.js json knex.js

我有这个 knexnest 查询:

    const query = knex.select([
      'notes.id AS _id',
      'notes.note AS _note',
      'notes.timestamp AS _timestamp',
      'customers.id AS _customerId',
      'users.name AS _name',
      'products.id AS _productId',
      'tags.id AS _tags_tagId',
      'tags.title AS _tags_tagTitle',
      'tags.type AS _tags_tagType',
    ]).from('notes')
      .join('users', 'notes.user_id', 'users.id')
      .join('customers', 'notes.customer_id', 'customers.id')
      .leftJoin('products', 'notes.product_id', 'products.id')
      .leftJoin('note_tags', 'notes.id', 'note_tags.note_id')
      .leftJoin('tags', 'note_tags.tag_id', 'tags.id')
      .where('customers.id', customerId);

    return knexnest(query);

我的响应 json 看起来像这样:

{
  "id": 47,
  "note": "This is an updated1 note",
  "timestamp": "2019-07-12T15:17:27.281Z",
  "customerId": 111781,
  "name": "Paul",
  "productId": 1,
  "tags": {
    "tagId": 4,
    "tagTitle": "price",
    "tagType": "product"
  }
}

问题是数据库返回了多个标签,只显示了一个。我期待这样的回应:

{
  "id": 47,
  "note": "This is an updated1 note",
  "timestamp": "2019-07-12T15:17:27.281Z",
  "customerId": 111781,
  "name": "Paul",
  "productId": 1,
  "tags": {[
    {
      "tagId": 4,
      "tagTitle": "price",
      "tagType": "product"
    },
    {
      "tagId": 5,
      "tagTitle": "quality",
      "tagType": "product"
    }
  ]}
}

我的查询是否有问题导致了这种情况?

最佳答案

明白了。我在标签中缺少双 __:

'tags.id AS _tags__tagId',
'tags.title AS _tags__tagTitle',
'tags.type AS _tags__tagType'

关于javascript - Knexnest 查询不返回数组中的数据,即使这是预期的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255618/

相关文章:

javascript - 当 SignalR 函数完成时,如何将 SignalR 函数的值返回到父函数?

javascript - 在 react-redux 应用程序状态的数据结构中有一个 didInvalidate 属性的目的是什么?

javascript - 仅使用 File 对象选择单个文件

Javascript 不等待 For 循环。对象值不变

PHP json_encode { } 字符

javascript - selectize.js - 如何禁用所选项目后闪烁的光标?

javascript - 在 Meteor 的钩子(Hook)中更改 react 变量的值

node.js - Angular 服务中的 "window is not defined"但代码运行良好

javascript - jQuery/JavaScript 循环遍历 json 数据结果

javascript - react Js : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0