如何在Loopback4中使用范围/关系

问题描述 投票:0回答:1

我正在开发一个社交应用程序,其中与用户模型(comment_by)建立了PostComments模型whos关系。

PostComments模型

import { Entity, model, property, belongsTo} from '@loopback/repository';
import {Users} from './users.model';

@model({settings: {strict: false, strictObjectIDCoercion: true, }})
export class PostComments extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  comment_id?: string;

  @property({
    type: 'string',
    required: true,
  })
  post_id: string;

  @property({
    type: 'string',
    required: true,
  })
  comment_text: string;

  @property({
    type: 'date',
    default: new Date(),
  })
  created_at?: string;

  @belongsTo(() => Users)
  comment_by: string;

  [prop: string]: any;

  constructor(data?: Partial<PostComments>) {
    super(data);
  }
}

export interface PostCommentsRelations {
  // describe navigational properties here
}

export type PostCommentsWithRelations = PostComments & PostCommentsRelations;

我需要在单个查询中获得所有注释及其用户名(注释为)的列表,并在下面的查询中进行尝试,该查询返回Users模型的所有字段,而我只需要来自用户的名称

this.postCommentsRepository.find({
  fields: {
    comment_id: true,
    post_id: true,
    comment_text: true,
    created_at: true,
    comment_by: true
  },
  include: [{ 
    relation: 'users',

  }],
  where: {
    post_id: post_id
  }
});

我也想使用loopback 3 features,但在回送4文档中找不到任何相对的东西

node.js loopbackjs loopback loopback4
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.