猫鼬打字稿:保存新记录

时间:2019-12-18 04:36:20

标签: javascript typescript mongoose

我有保存追随者的方案

import * as mongoose from 'mongoose';
import { Document, Schema } from 'mongoose';

interface FollowingModelInterface extends Document {
  followingId: mongoose.Types.ObjectId;
  userId: mongoose.Types.ObjectId;
}

const followingSchema: Schema = new Schema(
  {
    followingId: { type: mongoose.Types.ObjectId, index: true  },
    userId: { type: mongoose.Types.ObjectId, index: true },
  },
  {
    timestamps: true,
  }
);

const followingModel = mongoose.model<FollowingModelInterface>('Following', followingSchema);

export { followingModel, FollowingModelInterface };

然后我尝试将要保存的两个ID插入此模式

import { followingModel, FollowingModelInterface } from '../models/following';
...
const followed = new followingModel({
    followingId: { account._id },
    userId: { id },
    } as FollowingModelInterface);
const saved = await followed.save();

但是我得到了错误

Conversion of type '{ followingId: { account: UserModelInterface; (Missing): any; }; userId: { id: string; }; }' to type 'FollowingModelInterface' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Type '{ followingId: { account: UserModelInterface; (Missing): any; }; userId: { id: string; }; }' is missing the following properties from type 'FollowingModelInterface': createdAt, updatedAt, increment, model, and 56 more.ts(2352)

接口应该只是字符串类型吗?

0 个答案:

没有答案