【问题标题】:How to get data from relation's with data stored in array's of Id如何从与存储在 Id 数组中的数据的关系中获取数据
【发布时间】:2019-10-10 11:24:16
【问题描述】:

我有一个模型批次,其数据为

"id": {
      "type": "string",
      "id": true,
      "defaultFn": "uuidv4"
    },
    "batchType": {
      "type": "string",
      "required": true,
      "default": "COURSE"
    },
    "venue": {
      "type": "string",
      "required": true
    },

另一个模特说预定浴场

"batchesId": {
     "type": [
       "string"
     ],
     "required": true
   },

并创建了从批次模型到bookedBatches 模型的关系

 "relations": {
    "bookedBatches": {
      "type": "referencesMany",
      "model": "bookedBatches",
      "foreignKey": "batchesId",
      "primaryKey": "id"
    }
  }

现在我想要所有带有预订详细信息的批次,这些批次在批次模型的预订详细信息中存储为 ID 数组


let reqObject = {
                    "where": {
                        "and": [{
                            userId: userId,
                            isActive: true
                        }]
                    },
                    "order": "createdAt DESC",
                    include: [
                        {{
                            relation:"bookedBatches"}}]
}


Batches.find(reqObject, (resError, resData) => {
                    if (resError) {
                        return cb(new HttpErrors.BadRequest(resError, {
                            expose: false
                        }))
                    }
                    return cb(null, resData);
                })

但我没有得到任何价值,任何人都可以帮助通过关系获得价值

谢谢!

【问题讨论】:

    标签: mongodb foreign-keys relationship loopback


    【解决方案1】:

    我已经改进了您的代码。请试试这个

     let reqObject = {
      "where": {
        "and": [{
          userId: userId,
          isActive: true
        }]
      },
      "order": "createdAt DESC",
      include: [
        {
          relation: "bookedBatches",
          scope: { fields: ["id","batchesId"] }
        }
      ]
    }
    
    
    Batches.find(reqObject, (resError, resData) => {
      if (resError) {
        return cb(new HttpErrors.BadRequest(resError, {
          expose: false
        }))
      }
      return cb(null, resData);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2015-02-26
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多