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

编程入门 行业动态 更新时间:2024-10-13 02:21:30

如何在Loopback4中使用范围/<a href=https://www.elefans.com/category/jswz/34/1770737.html style=关系"/>

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

我正在开发一个社交应用程序,其中与用户模型(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文档中找不到任何相对的东西

回答如下:

更多推荐

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

本文发布于:2024-05-07 06:25:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!