“数组”类型的属性在领域对象服务器中具有未知的对象类型

时间:2017-10-02 05:55:16

标签: javascript node.js realm realm-mobile-platform

我正在尝试使用Realm mobile platform,我从领域对象服务器调用外部Web服务并创建不同实体的对象。

创建架构后我将数据转储到其中时出现以下错误

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!");
    });

1 个答案:

答案 0 :(得分:4)

问题在于:

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

Response指向Customer的链接,因此只要存在CustomerSchemaResponseSchema就必须包含在架构数组中。