m-cloud API Documentation - v1.0.1-beta.5
    Preparing search index...

    Class Controller

    Controller 基类 所有 Controller 层类的基类,提供请求处理功能 默认使用 'user' 集合,或根据类名自动推断集合名称 Controller

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    ctx: Context

    请求上下文

    db: Db

    UniCloud 数据库实例

    config: Record<string, unknown>

    配置对象

    service: Record<string, unknown>

    服务层实例

    controller: Record<string, unknown>

    控制器实例

    curl: <T = any>(
        url: RequestURL,
        options?: UrllibRequestOptions,
    ) => Promise<HttpClientResponse<T>>

    HTTP 请求方法(urllib)

    httpclient: HttpClient

    HTTP 客户端

    throw: (_message: string) => never & (
        _code: string | number,
        _message: string,
    ) => never

    错误抛出方法

    version: string

    API 版本

    apiVersion: string

    API 版本号

    no: (_message: string) => never & (
        _code: string | number,
        _message: string,
    ) => never

    错误抛出方法(别名)

    mdb: MdbOperations

    MongoDB 数据库操作工具

    plugin: Record<string, PluginUtility | unknown>

    插件访问器

    mdbName: string = 'user'

    默认集合名称,Controller 默认使用 'user' 集合

    Methods

    • Protected

      初始化 mdb 数据库操作工具 创建所有数据库操作方法,包括查询、插入、更新、删除、聚合等

      Parameters

      Returns void

    • Protected

      初始化插件访问器 创建插件代理对象,支持通过 this.plugin.{插件名称} 访问全局挂载的插件和工具函数

      Parameters

      Returns void

    • 从对象中选择指定的属性

      Parameters

      • obj: Record<string, unknown>

        源对象

      • keys: string | string[]

        属性名(可以是字符串或数组)

      Returns Record<string, unknown>

      包含选中属性的新对象

      const obj = { name: 'John', age: 30, city: 'New York' }
      const result = this.pick(obj, 'name age')
      // { name: 'John', age: 30 }

      const result2 = this.pick(obj, ['name', 'age'])
      // { name: 'John', age: 30 }
    • 返回成功的响应

      Parameters

      • Optionaldata: unknown

        响应数据

      • Optionalmessage: string

        响应消息

      Returns { code: number; message: string; data: unknown }

      成功响应对象

      // 返回数据
      this.ok({ id: '1', name: 'John' })
      // { code: 0, message: 'success', data: { id: '1', name: 'John' } }

      // 返回自定义消息
      this.ok({ id: '1' }, '创建成功')
      // { code: 0, message: '创建成功', data: { id: '1' } }

      // 返回空数据
      this.ok()
      // { code: 0, message: 'success', data: null }