简体   繁体   English

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

[英]Property of type 'array' has unknown object type in realm object server

I am trying to use Realm mobile platform where in i am calling external web service from realm object server and creating objects of different entities. 我正在尝试使用Realm mobile platform ,在该Realm mobile platform中,我从领域对象服务器调用外部Web服务并创建不同实体的对象。

after creating schema when i am dumping data into it i am getting following error 当我向其中转储数据时创建架构后,出现以下错误

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

here is my code 这是我的代码

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

The problem is with: 问题在于:

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

Response links to Customer , so CustomerSchema must be included in the schema array whenever ResponseSchema is present. Response链接到Customer ,因此,每当存在ResponseSchema时,必须将CustomerSchema包含在架构数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM