CastError: Cast to [ObjectId] failed for value怎么解决
发布于 9 个月前 作者 golmic 1013 次浏览 来自 问答

大家好,我在用Mongoose时遇到了这个问题,查Google没找到解决方案,请教大家。 这是我的schema

constructor() {
    mongoose.connect(
      config.mongoFullURI, { useMongoClient: true }
    );
    mongoose.Promise = global.Promise;
    const OrderSchema = new mongoose.Schema({
      username: String,
      password: String,
      link: String,
      startAt: Date,
      createdAt: Date,
      done: Boolean,
      creator: { type: mongoose.Schema.ObjectId, ref: 'user' },
    }, {
      collection: 'orders'
    });
    const UserSchema = new mongoose.Schema({
      wechat: {
        type: String,
        index: { unique: true }
      },
      createdAt: Date,
      purchasedTimes: Number,
      usedTimes: Number,
      orders: [{ type: mongoose.Schema.ObjectId, ref: 'order' }]
    }, {
      collection: 'users'
    });
    let order = mongoose.model(
      'order',
      OrderSchema
    );
    let user = mongoose.model(
      'user',
      UserSchema
    );
    this.OrderModel = order;
    this.UserModel = user;
  }

报错如下(node:5340) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): CastError: Cast to [ObjectId] failed for value "[{“username”:“asdas”,“password”:“adsfdsf”,“link”:"https://item.jd.com/3304295.html",“startAt”:“2017-11-15T09:17:40.000Z”}]" at path “orders”

1 回复

解决了

回到顶部