CNode

利用egg-mongoose在Model文件中定义的实例方法跟静态方法在service里该如何调用

问答
Kkuangyanit发布于7 年前最后回复7 年前2 回复3090 浏览0 收藏
'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)

K
kuangyanit#1·7 年前

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

M
muzi131313#2·7 年前

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

参与回复
登录后即可参与回复。登录