将数据从前端推送到后端的 MongoDB

时间:2020-12-19 07:11:53

标签: node.js mongodb typescript express mongoose

我从前端接收信息并将其传递给我的快速服务器。我在整个代码中都遵循路由-控制器-服务架构。

我想进行数据库查询以将该信息从前端插入到我的 MongoDB 数据库中。

我的模型 - AppMakerConfig.model.ts

import { Types } from "mongoose";
import { getModelForClass, getName, prop, Ref } from "@typegoose/typegoose";
import { AppMakerSettings } from "./AppMakerSettings.model";

export enum Status {
  ACTIVE = "ACTIVE",
  PENDING = "PENDING",
  REJECTED = "REJECTED"
}

export class AppMakerConfig {
  @prop({ required: true, unique: true })
  public appName: string;

  @prop({ required: true })
  public appLogo: string;

  @prop({ required: true })
  public about: string;

  @prop({ enum: Status, default: "ACTIVE" })
  public status: Status;

  @prop({ type: Types.ObjectId, ref: "Users" })
  public user: Ref<unknown>;

  @prop({ type: Types.ObjectId, ref: getName(AppMakerSettings) })
  public settings: Ref<AppMakerSettings>[];
}

export const AppMakerConfigModel = getModelForClass(AppMakerConfig);

然后我有我的服务文件,其中包含来自用户的所有信息 - index.ts

import { Service, Inject } from "typedi";

import { AppMakerConfigModel } from "../../models/appMaker/AppMakerConfig.model";
import MobileAppService from "../mobileApp.service";

@Service("AppMakerService")
class AppMakerService {
  public async addToAppMakerDB(appMakerDetails: any) {
    console.log("AppMaker Details", appMakerDetails);
    return appMakerDetails;
  }
}

export default AppMakerService;

appMakerDetails 包含模型中的所有字段。

如何编写查询以将信息从服务器请求插入模型?

提前致谢。

0 个答案:

没有答案