How parse xml http get return before insert mongo?

Hi, i use this method to get xml return an http.get

you can see xml return with: view-source:http://api.eve-central.com/api/marketstat?typeid=29668&regionlimit=10000002&hours=12

maybe it is better to use xml -> json ?

Thankkkks
`
Meteor.methods({
eveapiinsert: function () {
var self = this;
try {
var response = HTTP.get(“http://api.eve-central.com/api/marketstat?typeid=29668&regionlimit=10000002&hours=12”);
//console.log(response.content);
//var result = xml2json(response.content);
console.log(response.content);
//session.subscribe(‘posts’);
//Posts.insert(result);
//this.ready();
//return Posts.find();

}

catch(error) {
	console.log(error);
}

}
})

`

I come to you to seek your opinion,

i get every time:

`Exception in callback of async function: Error: key $ must not start with ‘$’

How to solve this?

Thankks
`

i dont understand how can i parse object before mongodb insert

console.log(resulta.evec_api.marketstat); —> return -> type: [ [Object] ]

Meteor.methods({
xmlDemo: function () {
var self = this;
try {
HTTP.call( 'GET', 'http://api.eve-central.com/api/marketstat?typeid=29668&regionlimit=10000002&hours=12', {}, function( error, response ) {
  if ( error ) {
    console.log( error );
  } else {
	var resulta = xml2js.parseStringSync(response.content);
	console.log(resulta.evec_api.marketstat);
  }
});
}

catch(error) {
	console.log(error);
}
}
})

The key to answering this is to make sure you clearly understand what you’re getting back. As you have deduced, a good way to do this is to console.log the results. However, on its own console.log is very helpful. Here’s what I did to understand what is going on (and the result is maybe not quite what you thought). Note the use of JSON.stringify to get a more helpful log.

in server/main.js

import { Meteor } from 'meteor/meteor';
import { xml2js } from 'meteor/peerlibrary:xml2js';

Meteor.startup(() => {
  try {
    const response = HTTP.get("http://api.eve-central.com/api/marketstat?typeid=29668&regionlimit=10000002&hours=12");
    const result = xml2js.parseStringSync(response.content);
    console.log(JSON.stringify(result,null,2));
  } catch (error) {
    console.log(error);
  }
});

As you will see, this is not in a method and no client code is needed. Starting meteor results in this:

{
  "evec_api": {
    "$": {
      "version": "2.0",
      "method": "marketstat_xml"
    },
    "marketstat": [
      {
        "type": [
          {
            "$": {
              "id": "29668"
            },
            "buy": [
              {
                "volume": [
                  "1235"
                ],
                "avg": [
                  "862555051.24"
                ],
                "max": [
                  "1047800000.00"
                ],
                "min": [
                  "1999995.99"
                ],
                "stddev": [
                  "435301531.53"
                ],
                "median": [
                  "1013300001.00"
                ],
                "percentile": [
                  "1046890915.49"
                ]
              }
            ],
            "sell": [
              {
                "volume": [
                  "2692"
                ],
                "avg": [
                  "1225511768.79"
                ],
                "max": [
                  "2010000000.00"
                ],
                "min": [
                  "1047800000.00"
                ],
                "stddev": [
                  "125954124.78"
                ],
                "median": [
                  "1225500000.00"
                ],
                "percentile": [
                  "1051382202.56"
                ]
              }
            ],
            "all": [
              {
                "volume": [
                  "223854"
                ],
                "avg": [
                  "8654.89"
                ],
                "max": [
                  "16597485.32"
                ],
                "min": [
                  "0.30"
                ],
                "stddev": [
                  "3634873.14"
                ],
                "median": [
                  "1.99"
                ],
                "percentile": [
                  "1.00"
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

Hopefully, that will point you in the right direction to a solution, but if not, please shout up! :slight_smile:

1 Like