【问题标题】:Knexnest query not returning data in array even though this is what is expected即使这是预期的,Knexnest 查询也不返回数组中的数据
【发布时间】:2019-07-29 14:09:46
【问题描述】:

我有这个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"
    }
  ]}
}

我的查询中有什么错误导致了这种情况吗?

【问题讨论】:

    标签: javascript node.js json knex.js


    【解决方案1】:

    知道了。我在标签中缺少双 __

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

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 2020-11-12
      相关资源
      最近更新 更多