我正在使用打字稿创建这样的猫鼬模型
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。