0

我无法使用关系对象发布数据 json。

我使用 mongoDB。
我有 3 个表:table_1、table_2、table_3。

我创建关系EmbedsManyEmbedsOne
- table_2 EmbedsOnetable_1。
-EmbedsMany表_2 表_3。

我不知道创建发布数据 json 以使用 table_1 的项目创建 table_2 的新项目。

import { ..., embedsMany, embedsOne } from '@loopback/repository';
import { Model1, Mode1WithRelations } from './model-1.model';
import { Model3, Model3WithRelations } from './model-2.model';
@model({
    settings: {
        strictObjectIDCoercion: true,
        mongodb: {
            collection: 'table_2'
        }
    }
})
export class Model2 extends Entity {
    @property({
        type: 'string',
        id: true,
        mongodb: {
            dataType: 'ObjectID' // or perhaps 'objectid'?
        }
    })
    id?: string;

    @embedsMany(() => Model3)
    model3?: Model3[];

    @embedsOne(() => Model1)
    model1: Model1;
}

export interface Model2Relations {
    // describe navigational properties here
    model3?: Model3WithRelations[];
    model1: Mode1WithRelations;
}

export type Model2WithRelations = Model2 & Model2Relations;

存储库模型 2

import { DefaultCrudRepository } from '@loopback/repository';
import { Model2, Model2Relations } from '../models';
import { DbDataSource } from '../datasources';
import { inject } from '@loopback/core';

export class Model2Repository extends DefaultCrudRepository<
    Model2,
    typeof Model2.prototype.id,
    Model2Relations
    > {
    constructor(
        @inject('datasources.DB') dataSource: DbDataSource,
    ) {
        super(Model2, dataSource);
    }
}

Json数据发布

{
  "address": "string",
  "status": 1,
  "createdAt": "2019-08-04T03:57:12.999Z",
  "updatedAt": "2019-08-04T03:57:12.999Z",
  "model1": {
     "id": "5d465b4cd91e484250d1e54b" /* id of exist item in table_1 */
  }
}

控制器由生成lb4 controller

预期:
- 项目成功保存到 table_2 中,EmbedsOne项目为 table_1。

实际:
- 错误:

{
    "error": {
        "statusCode": 422,
        "name": "ValidationError",
        "message": "The `Model2` instance is not valid. Details: `model1` is not defined in the model (value: undefined).",
        "details": {
            "context": "Model2",
            "codes": {
                "project": ["unknown-property"]
            },
            "messages": {
                "model1": ["is not defined in the model"]
            }
        }
    }
}
4

1 回答 1

0

TL;DR 根据Loopback 4团队的说法, @embedsOne @embedsMany @referencesOne @referencesMany 尚未实施(2019 年 9 月 3 日)。(参见文档github

但是我在源代码上找到了这些装饰器和它们的类 所以,希望我们必须等到实现完成。如果我有任何新内容,我会尝试更新这个答案。

于 2019-09-03T15:59:24.333 回答