Feathers-mongoose : 通过feathers-mongoose中的自定义属性获取

Feathers-mongoose : Get by custom attribute in feathers-mongoose

我有一个非常基本的羽毛服务,它使用 feathers-mongoose 包将数据存储在猫鼬中。问题在于获取功能。我的模型如下:

module.exports = function (app) {
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;
  const messages = new Schema({
    message: { type: String, required: true }
  }, {
    timestamps: true
  });

  return mongooseClient.model('messages', messages);
};

当用户 运行 发出 GET 命令时:

curl http://localhost:3030/messages/test

我有以下要求

  1. 这实质上是尝试将测试转换为 ObjectID。我会怎样 喜欢它做的是 运行 针对消息属性的查询 {message : "test"} ,我不确定如何实现。有 没有足够的文档来理解编写或更改它 在钩子里。有人可以帮忙吗
  2. 我想 return 自定义错误代码 (http),当某行未找到或与我的某些条件不匹配时。我怎样才能做到这一点?

谢谢

在 Feathers before hook you can set context.result 中,在这种情况下将跳过原始数据库调用。所以流量是

  • 在之前的 get 挂钩中,尝试按名称查找消息
  • 如果存在,将 context.result 设置为已找到的
  • 否则什么都不做,这将 return 原来的 get by id

这是它的样子:

async context => {
  const messages = context.service.find({
    ...context.params,
    query: {
      $limit: 1,
      name: context.id
    }
  });

  if (messages.total > 0) {
    context.result = messages.data[0];
  }

  return context;
}

Errors API.

中记录了如何创建自定义错误和设置错误代码