2017-10-02 6 views
1

Realm mobile platform私はrealmオブジェクトサーバから外部Webサービスを呼び出し、異なるエンティティのオブジェクトを作成しています。タイプ 'array'のプロパティは、オブジェクトサーバで不明なオブジェクトタイプを持っています

私はそれにデータをダンプしていたときにスキーマを作成した後、私はここで、次のエラー

Schema validation failed due to the following errors: 
- Property 'Response.customersList' of type 'array' has unknown object type 'Customer' 

を取得しています私のコードは、問題がである

'use strict'; 

    var express = require('express'); 
    var request = require('request'); 
    var rp = require('request-promise'); 
    var Realm = require('realm'); 
    var app = express(); 

    class Customer {} 
    let 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} 
     } 
    }; 

    let ResponseSchema = { 
     name: 'Response', 
     properties: { 
     errorMessage: 'string', 
     status: 'string', 
     customersList: {type: 'list',objectType:'Customer'} 
     } 
    }; 


    app.get('/getMasterData', (req, res) => { 

    console.log("Web service called"); 

    const options = { 
     method: 'POST', 
     uri: 'url', 
     body: { 
     userName: '4500858', 
     password: 'password', 
     companyId: 'UST1', 
     page: 1, 
     }, 
     headers: { 
     'content-type': 'application/json' 
     }, 
     json: true 

    }; 

    rp(options) 
     .then(function (response) { 


     try{ 

      var customerList = response.customersList; 
      var responseRealm = new Realm({   
       schema: [ResponseSchema] 
      }); 

      // error comes in following block 
      responseRealm.write(() => { 

        console.log("inserting"); 
        responseRealm.create('Response',response, true); 
      });  
     }catch(e){ 

       console.log("Error 1"+e.message); 

     } 


     }) 
     .catch(function (err) { 

     console.log("Error 2"+JSON.stringify(err)); 

     }); 


    }); 

    function initializeSchema(){ 

     Realm.open({schema: [CustomerSchema, ResponseSchema], 
        schemaVersion: 1, 
        migration: (oldRealm, newRealm) => { 

         console.log("oldRealm "+oldRealm); 
          if (oldRealm.schemaVersion < 1) { 


          } 
        } 
      }).then(realm => { 
      console.log("Schema open"); 

      let customers = realm.objects('Customer'); 
      console.log(customers.length); 

      for(let i=0;i<customers.length;i++){ 

       console.log(customers[i].firstName); 
      } 

     }); 

    } 

    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(); 
    } 

    initializeSchema(); 

    app.listen(3000, function() { 
     console.log("Go!"); 
    }); 
+0

これは 'java'コードではありません。タグを修正してください。 –

答えて

1

です:

var responseRealm = new Realm({ 
    schema: [ResponseSchema] 
}); 

ResponseCustomerにリンクするので、CustomerSchemaを含む必要がありますスキーマ配列はいつでもResponseSchemaが存在します。

関連する問題