转换为对象时如何键入猫鼬模型

时间:2020-12-29 10:45:00

标签: javascript typescript mongoose

我正在使用打字稿创建这样的猫鼬模型

import mongoose, { Schema, Document } from "mongoose";
 
interface ItemModel extends Document {
  fullName: string;
  identifier: string;
}

const ItemSchema: Schema = new mongoose.Schema({
  fullName: {
    type: String,
  },
  identifier: {
    type: String,
  }
});

export const Item = mongoose.model<ItemModel>("item",ItemSchema);

比我创建的函数应该搜索mongodb中是否已经存在这样的项目

function alreadyExistInDb(identifier: string) {
  return new Promise<{} | false>((resolve, reject) => {
    Item.findOne({ identifier: identifier }, function (err, result) {
      if (result) { //result here is of type ItemModel
        resolve(result.toObject());
      } else {
        resolve(false);
      }
    });
  });
}

这里我不确定如何在 Promise 中输入 result.toObject。

0 个答案:

没有答案