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

时间:2017-10-03 07:28:55

标签: realm realm-mobile-platform

当我尝试使用函数在领域架构中设置default值时

mCustomerId: {type: 'string', default: CreateGuid()}

我收到以下错误

  

Customer.mCustomerId必须是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并将其插入到realm(它是一个业务逻辑,我们不能将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();  
    } 

那么我将如何在领域javascript

中实现设置自定义guid

1 个答案:

答案 0 :(得分:0)

您无法指定要调用的函数作为默认值。我绝对可以看到它的用例。请随意在https://github.com/realm/realm-js打开问题。