使用函数设置模式中字段的默认值

use function to set default value of a field inside schema

当我尝试使用函数在领域架构

中设置 default 值时
mCustomerId: {type: 'string', default: CreateGuid()}

我收到以下错误

Customer.mCustomerId must be of type: string

让 CustomerSchema ={

  name: 'Customer',
  primaryKey: 'mCustomerId',
  properties: {
    errorMessage:  {type: 'string', optional: true},
    status:{type: 'string', optional: true},
    address: {type: 'string', optional: true},
    customerNumber: {type: 'string', optional: true},
    city: {type: 'string', optional: true},
    contactId: {type: 'double', optional: true},
    country: {type: 'string', optional: true},
    customerId:{type: 'double', optional: true},
    customerRecordType: {type: 'string', optional: true},
    currency: {type: 'string', optional: true},
    email: {type: 'string', optional: true},
    custGroup: {type: 'string', optional: true},
    invoiceAndDeliveryOnHold: {type: 'string', optional: true},
    estimate: {type: 'string', optional: true},
    fax: {type: 'string', optional: true},
    firstName:{type: 'string', optional: true},
    lastName: {type: 'string', optional: true}, 
    mCustomerId: {type: 'string', default: CreateGuid()},   
    notes: {type: 'string', optional: true},    
    organizationName: {type: 'string', optional: true},
    phoneNumber: {type: 'string', optional: true},
    department: {type: 'string', optional: true},
    parentCompanyId: {type: 'double', optional: true},
    parentCompanyName: {type: 'string', optional: true},
    state: {type: 'string', optional: true},
    totalInvoiced: {type: 'double', optional: true},    
    zipcode: {type: 'string', optional: true},
    customerStatus: {type: 'string', optional: true},
    siteId: {type: 'string', optional: true},
    wareHouseId:{type: 'string', optional: true}
  }
};

我正在从服务器获取数据,其中 mCustomerId 将是 null 所以我们想在我们的最后生成 guid 并将其插入领域(这是一个业务逻辑,我们不能将 customerId 作为主键所以请求不要建议此选项)

function CreateGuid() {  

        function _p8(s) {  
          var p = (Math.random().toString(16)+"000000000").substr(2,8);  
          return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ;  
        }  
        return _p8() + _p8(true) + _p8(true) + _p8();  
    } 

那么我该如何实现在领域中设置自定义 guid javascript

您不能将要调用的函数指定为默认值。我绝对可以看到它的用例。欢迎在 https://github.com/realm/realm-js.

打开一个问题