猫鼬和ref UUID的数组不转换 [英] Mongoose and array of ref UUID's does not convert

查看:50
本文介绍了猫鼬和ref UUID的数组不转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mongoose-uuid库时,我可以为自己的模式设置UUID类型,因此当我读取数据时,它是字符串(utf-8)格式,而当我保存数据时,它是UUID ObjectID BSON类型4格式.这与我的架构中的顶级或统一直接值和ref定义一起使用时效果很好.但是,当我在架构的ref数组中有一个UUID时,该数组会正确保存到数据库中,但是在显示时它是原始类型.根据下面的示例,您可以看到scope_id以正确的格式显示,但权利却没有.

When using the library mongoose-uuid, I am able to setup UUID types for my schemas, so when I read the data it is in string (utf-8) format and when I save the data it is in UUID ObjectID BSON type 4 format. This works great with top level or flat direct values and ref definitions in my schema. However, when I have a UUID's in an array of ref's in a schema, the array saves to the database correctly, However when it is presented it is in its raw type. Based on the example below you can see scope_id is presented in the right format but the entitlements are not.

以下是我使用的版本: 猫鼬-uuid-2.3.0 猫鼬-5.5.11

Here are the versions I am using: mongoose-uuid - 2.3.0 mongoose - 5.5.11

我尝试通过更改getter并转换值来修改库(mongoose-uuid),但是,当我这样做时,它在呈现时可以工作,但是在保存到数据库时会失败.这很可能是由于值在保存到数据库之前已转换或强制转换.

I have tried modifying the library (mongoose-uuid) by changing the getter and converting the value, however, when I do so, it works when presenting but fails when it saves to the database. This is most likely due to the fact that the value is converted or casted before saving to the database.

这是示例架构

    {
      "code": {
        "type": String,
        "required": true
      }, 
      "scope_id": {
        "type": mongoose.Types.UUID,
        "ref": "scopes"
      },
      "entitlements": [{
        "type": mongoose.Types.UUID,
        "ref": "entitlements"
      }]
    }

实际响应示例

{
    "entitlements": [
        "zMihi1BKRomM1Q41p7hgLA==",
        "ztOYL7n1RoGA6aoc0TcqoQ=="
    ],
    "code": "APPUSR",
    "scope_id": "b8f80c82-8325-4ffd-bfd7-e373a90e7c45",
    "id": "32e79061-e531-45ad-b934-56811e2ad713"
}

预期响应

{
    "entitlements": [
        "ccc8a18b-504a-4689-8cd5-0e35a7b8602c",
        "ced3982f-b9f5-4681-80e9-aa1cd1372aa1"
    ],
    "code": "APPUSR",
    "scope_id": "b8f80c82-8325-4ffd-bfd7-e373a90e7c45",
    "id": "32e79061-e531-45ad-b934-56811e2ad713"
}

推荐答案

如上所述,该代码可以正常工作,但会破坏代码的另一部分.我找到了纠正此问题的解决方案:

As mentioned above, the code does work but breaks another part of the code. I found a solution that corrects this:

这是对上面代码的略微修改

It is a slight amendment to the code above

SchemaUUID.prototype.cast = function (value, doc, init) {

console.log("cast", value, doc, init)

  if (value instanceof mongoose.Types.Buffer.Binary) {
    if (init && doc instanceof mongoose.Types.Embedded) {
      return getter(value);
    }
    return value;
  }

  if (typeof value === 'string') {
    var uuidBuffer = new mongoose.Types.Buffer(uuidParse.parse(value));

    uuidBuffer.subtype(bson.Binary.SUBTYPE_UUID);

    return uuidBuffer.toObject();
  }

  throw new Error('Could not cast ' + value + ' to UUID.');
};

此代码的替代版本允许应用诸如POST和PATCH之类的更新.

This alternate version of the code allows for updates such as POST and PATCH to be applied.

这篇关于猫鼬和ref UUID的数组不转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆