模式上数组内部的数组

时间:2018-08-02 15:36:54

标签: arrays schema connector google-data-studio

我正在为Data Studio开发连接器,并且具有以下架构:

            var azulDataSchema = [
              {
                name: 'NUMBER',
                label: 'SALE NUMBER',
                dataType: 'STRING',
                semantics: {
                  conceptType: 'DIMENSION'
                }
              },
              {
                name: 'STATUS',
                label: 'SALE STATUS',
                dataType: 'STRING',
                semantics: {
                  conceptType: 'DIMENSION'
                }
              }, 
              {
                name: 'CUSTOMER_NAME',
                label: 'CUSTOMER NAME',
                dataType: 'STRING',
                semantics: {
                  conceptType: 'DIMENSION'
                }
              },  
              {
                name: 'PAYMENT_INSTALLMENTS_NUMBER',
                label: 'PAYMENT_INSTALLMENTS_NUMBER',
                dataType: 'STRING',
                semantics: {
                  conceptType: 'DIMENSION',      
                }
              },
              {
                name: 'PAYMENT_INSTALLMENTS_VALUE',
                label: 'PAYMENT_INSTALLMENTS_VALUE',
                dataType: 'NUMBER',
                semantics: {
                  conceptType: 'METRIC',
                  isReaggregatable: true
                }
              },
              {
                name: 'PAYMENT_INSTALLMENTS_STATUS',
                label: 'PAYMENT_INSTALLMENTS_STATUS',
                dataType: 'STRING',
                semantics: {
                  conceptType: 'DIMENSION'
                }
              }
            ]
  

因此,对于一次销售(由NUMBER标识),我可以有一个或更多   付款方式。

        **The response of API is:**
        [
           {
              "id":"58fe71af-a9d5-410d-b729-db9b2c7494d3",
              "number":3,
              "status":"COMMITTED",
              "customer":{
                 "name":"CLIENTE JOSÉ"
              },
              "payment":{
                 "installments":[
                    {
                       "number":1,
                       "value":8,
                       "status":"PENDING"
                    },
                    {
                       "number":2,
                       "value":8,
                       "status":"PENDING"
                    },
                    {
                       "number":3,
                       "value":8,
                       "status":"PENDING"
                    },
                    {
                       "number":4,
                       "value":8,
                       "status":"PENDING"
                    },
                    {
                       "number":5,
                       "value":8,
                       "status":"PENDING"
                    }
                 ]
              },
              "total":80
           }
        ]
  

我为Data Studio传递数据的功能是:

function getData(request) {  
      var dataSchema = [];          

      request.fields.forEach(function(field) {
        for (var i = 0; i < azulDataSchema.length; i++) {
          if (azulDataSchema[i].name === field.name) {
            dataSchema.push(azulDataSchema[i]);
            break;
          }
        }
      });     

      var response = scriptProperties.getProperty("Data");    
      response = JSON.parse(response);     

      var data = [];            


  try{
    //begin object keys foreach
    Object.keys(response).forEach(function(sale) {       
      var values = [];      

      //begin dataSchema foreach
      dataSchema.forEach(function(field) { 

        switch(field.name){          
          case 'NUMBER':          
            if(response[sale].number != undefined && response[sale].number != null){             
              values.push(response[sale].number.toString());
            }
            break;         
          case 'STATUS':
            if(response[sale].status != undefined &&
               response[sale].status != null){             
              values.push(response[sale].status); 
            }          
            break;
          case 'CUSTOMER_NAME':     
            if(response[sale].customer != undefined &&
               response[sale].customer != null){              
              values.push(response[sale].customer.name);
            }
            break;         

           //BEGIN INSTALLMENTS  
            //*******FROM HERE, SOMETHING IS NOT CORRECT*****
          case 'PAYMENT_INSTALLMENTS_NUMBER' :
            if(response[sale].payment.installments != null){  
               response[sale].payment.installments.forEach(function (installmentNumber){                                 
                values.push(installmentNumber.number.toString());                                  
              });  
            }
            break;            
          case 'PAYMENT_INSTALLMENTS_VALUE':
            if(response[sale].payment.installments != null){        
              response[sale].payment.installments.forEach(function (installmentValue){                        
                values.push(installmentValue.value);   
              });                       
            }
            break;          
          case 'PAYMENT_INSTALLMENTS_STATUS':
            if(response[sale].payment.installments != null){  
              response[sale].payment.installments.forEach(function (installmentStatus){                            
               values.push(installmentStatus.status);
              });              
            }
            break;

            //END INSTALLMENTS
            case 'TOTAL':
            if(response[sale].total != undefined){

              values.push(response[sale].total); 
            }else{
              values.push(0);
            }          
            break;    
          default:          
            values.push("");

        }//END swith case

      }); //END dataSchema foreach   

      data.push({
        values: values,
      });

    });//END object keys foreach

  }catch(error){
    Logger.log('ERRO: ' + error);    
  }
      return {
        schema: dataSchema,
        rows: data
      };
    };//END function
  

将数据传递到Data Studio的方式是:

[
   {
      "values":[
         "JOSÉ", //customer name
         "3", //number of sale
         "1", //installment number (of this sale)
         "2", //installment number (of this sale)
         "3", //installment number (of this sale)
         "4", //installment number (of this sale)
         "5", //installment number (of this sale)
         "PENDING", //installment status (of this sale)
         "PENDING", //installment status (of this sale)
         "PENDING", //installment status (of this sale)
         "PENDING", //installment status (of this sale)
         "PENDING", //installment status (of this sale)
         8, //installment value (of this sale)
         8, //installment value (of this sale)
         8, //installment value (of this sale)
         8, //installment value (of this sale)
         8, //installment value (of this sale)
         "COMMITTED", //sale status
         80 //total sale
      ]
   }
]
  

因此,对于forEach,我试图传递一次销售的INSTALLMENTS列表,但是当我尝试在Data Studio中查看它时,我得到了错误代码   (0e99578d):[错误数据工作室] [1]

      [1]: https://i.stack.imgur.com/hxTED.png

Help me please rs

Thanks

1 个答案:

答案 0 :(得分:1)

import React, { Component } from 'react'; import Ticket from '../components/Ticket.js' import TicketForm from '../components/TicketForm.js' import { DB_CONFIG } from '../config/config.js'; import firebase from 'firebase/app'; import 'firebase/database'; // CSS import '../assets/css/TicketList.css'; import '../assets/css/App.css'; class TicketList extends Component { constructor(props){ super(props); this.addTicket = this.addTicket.bind(this); this.removeTicket = this.removeTicket.bind(this); if (!firebase.apps.length) { this.app = firebase.initializeApp(DB_CONFIG); this.database = this.app.database().ref().child('tickets'); } // We're going to setup the React state of our component this.state = { tickets: [], } } componentWillMount(){ const previousTickets = this.state.tickets; // DataSnapshot this.database.on('child_added', snap => { previousTickets.push({ id: snap.key, ticketContent: snap.val().ticketContent, }) this.setState({ tickets: previousTickets }) }) this.database.on('child_removed', snap => { for(var i=0; i<previousTickets.length; i++){ if(previousTickets[i].id === snap.key){ previousTickets.splice(i, 1); } } this.setState({ tickets: previousTickets }) }) } addTicket(ticket){ this.database.push().set({ ticketContent: ticket }); } removeTicket(ticketId){ this.database.child(ticketId).remove(); } render() { return ( <div> <div className="m-container"> <h1>Open Questions</h1> <hr/> </div> <div> { this.state.tickets.map((ticket) => { return ( <Ticket ticketContent={ticket.ticketContent} ticketId={ticket.id} key={ticket.id} removeTicket={this.removeTicket} /> ) }) } </div> <div> <TicketForm addTicket={this.addTicket} /> </div> </div> ); } } export default TicketList; 应该具有从getData到值的一对一映射,并且值必须是标量(即field.namestringnumber)。如果boolean请求中有3个fields,则您的响应应包含三个条目。 (即类似getData的东西)当前,您的{"values":[1, 2, 3]}响应在模式期望为6时返回19个条目。

对于您的特定用例,有两种解决方法。

为每个分期付款编号,状态和值创建新行。

按照这种方法,您的getData响应类似于以下内容:

getData

汇总您的字段

如果对您的数据有意义,则可以通过求和,求平均或通过其他任何聚合来汇总这些字段,这些汇总采用每个架构字段的多个值并将其转换为标量值。

按照这种方法,您的[ { "values": [ "JOSÉ", //customer name "3", //number of sale "1", //installment number (of this sale) "PENDING", //installment status (of this sale) 8, //installment value (of this sale) "COMMITTED", //sale status 80 //total sale ] }, { "values": [ "JOSÉ", //customer name "3", //number of sale "2", //installment number (of this sale) "PENDING", //installment status (of this sale) 8, //installment value (of this sale) "COMMITTED", //sale status 80 //total sale ] }, { "values": [ "JOSÉ", //customer name "3", //number of sale "3", //installment number (of this sale) "PENDING", //installment status (of this sale) 8, //installment value (of this sale) "COMMITTED", //sale status 80 //total sale ] }, { "values": [ "JOSÉ", //customer name "3", //number of sale "4", //installment number (of this sale) "PENDING", //installment status (of this sale) 8, //installment value (of this sale) "COMMITTED", //sale status 80 //total sale ] }, { "values": [ "JOSÉ", //customer name "3", //number of sale "5", //installment number (of this sale) "PENDING", //installment status (of this sale) 8, //installment value (of this sale) "COMMITTED", //sale status 80 //total sale ] } ] 响应看起来像这样:

getData

请注意,我删除了分期付款编号条目,因为如果将这些值进行汇总,则可能没有意义。

有关其他任何详细信息,请参见Data Studio社区连接器参考中的getData