利用egg-mongoose在Model文件中定义的实例方法跟静态方法在service里该如何调用
发布于 5 年前 作者 kuangyanit 2429 次浏览 来自 问答
'use strict';

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const EventSchema = new Schema({
    type: { type: String },
    openid: { type: String },
    data: { type: Object },
    dateCreated: { type: Date },
    equipment: { type: Schema.Types.ObjectId, ref: 'Equipment' },
  });
  
  EventSchema.methods.getItem1 = (_id, ctx) => {
    return ctx.model.Event.findById(_id);
  };

  EventSchema.statics.getItem2 = (_id, ctx) => {
    return ctx.model.Event.findById(_id);
  };

  const Event = mongoose.model('Event', EventSchema, null, { cache: false });
  
  return Event;
};

这里的getItem1 / getItem2 方法在service中该如何调用?

2 回复

搜了半天没搜到例子,希望可以得到老哥们的解答。

getItem1、getItems2这些操作不是应该放到service中操作么?如果你想这样操作,应该挂到Event上吧

回到顶部