属性 'Template.TemplateId' 类型 'object' 具有未知对象类型 'Decimal128'

Property 'Template.TemplateId' of type 'object' has unknown object type 'Decimal128'

我正在尝试使用 Realm React Native SDK,我正在尝试创建一个具有 long int 类型的 ID 的数据集。正如领域文档中所述,我在我的模式中使用 Decimal128 类型,但它会抛出以下错误,

Property 'Template.TemplateId' of type 'object' has unknown object type 'Decimal128'

代码如下:

const TemplateSchema = {
  name: "Template",
  properties: {
    Description: "string",
    Status: "int",
    TemplateCode: "string",
    TemplateId: "Decimal128",
    TemplateName: "string",
  },
};

let newTemplates;
realm.write(() => {
  newTemplate = realm.create("STGTemplate", {
    Description: "TEST Desc",
    Status: 0,
    TemplateCode: "TEST 01",
    TemplateId: 1486202620914,
    TemplateName: "Test Template",
  });
});

decimal128 是一个高精度的十进制数,它看起来不像你存储的那样

TemplateId: 1486202620914,

首先,查看 Field Types 文档以参考选项。请注意 decimal128 用于高精度数字,例如很多小数位,是一个很好的金钱解决方案

BSON.Decimal128 类型表示一个 128 位(16 字节)的浮点数。此类型适用于十进制值必须精确舍入的用例,例如财务数据

BSON.Decimal128 Quick Start

在您的情况下,您可以使用双精度数,因为它在内部存储为 64 位数字(如果需要,则为十进制数),这足以满足此用例。最初我建议使用 double,但如果不需要 decimal,则 int 可能是代码清晰度的最佳解决方案。它们都映射到 Javascript 数字。