Return posts find into meteor publish?

hi, would you know give me the best procedure to return a collection to the client with a “Meteor.publish”?

server/Server.js
Posts = new Mongo.Collection(‘posts’);
`Meteor.publish(‘mypublish’, function() {
var self = this;

try {
	var response = HTTP.get("http://linkforapicall-returnxml");
	console.log(response.content);
	Posts.insert(response);
	this.ready();
	return Posts.find();

}

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

});`

Client Side

Template.mytemplate.onCreated(function() { Meteor.subscribe('mypublish'); });

`

My Posts Collection


{{#each posts}}
{{content}}

{{/each}}

`

Thank you in advance for your help

db.posts

{ "_id" : "z4csinx9QLhMBKYcd", "statusCode" : 200, "content" : "<?xml version='1.0' encoding='utf-8'?>\n<evec_api version=\"2.0\" method=\"marketstat_xml\">\n <marketstat><type id=\"29668\">\n <buy><volume>1474</volume><avg>920928220.51</avg><max>1071000000.01</max><min>2300000.00</min><stddev>414107437.77</stddev><median>1051500000.01</median><percentile>1068717569.59</percentile></buy>\n <sell><volume>2485</volume><avg>1241428744.38</avg><max>2010000000.00</max><min>1086300000.02</min><stddev>116355540.29</stddev><median>1250000000.00</median><percentile>1099672004.46</percentile></sell>\n <all><volume>224152</volume><avg>8584.42</avg><max>16597485.32</max><min>0.30</min><stddev>3651679.64</stddev><median>1.99</median><percentile>1.00</percentile></all>\n </type></marketstat>\n </evec_api>", "headers" : { "server" : "nginx", "date" : "Thu, 31 Mar 2016 13:32:21 GMT", "content-type" : "text/xml; charset=UTF-8", "content-length" : "767", "connection" : "keep-alive", "vary" : "Accept-Encoding", "access-control-allow-origin" : "*" }, "data" : null }

You need to start by putting Posts = new Mongo.Collection('posts'); into a shared folder - available to the client and the server. In “classic” Meteor that may be in a lib/ folder, for example.

Additionally, publications are not in themselves reactive, so in the example you show, the publication code will be executed once only each time the server is restarted.

You will also need a template helper to satisfy your template’s {{content}}. Something like

Template.mytemplate.helpers({
  content() {
    return Posts.find();
  }
});
1 Like

{{#each content}} <div>{{content}}</div> {{/each}}

return into mytemplate.html

[object Object] [object Object] [object Object] [object Object]

what i’m doing wrong ? :sweat_smile:

OK - I missed that bit off :slight_smile:

{{#each content}}
  <div>{{status_code}} {{date}} {{connection}} ...</div>
{{/each}}
1 Like

thank you very much :christmas_tree:

1 Like

I would also suggest that unless you need the raw XML, you consider converting it to JSON (it does not look as though it will lose fidelity). The peerlibrary:xml2js package is worth looking at.

something like that ?

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

}

`

Return : [Error: key $ must not start with ‘$’]

did I miss something?

sources: [SOLVED] Parsing XML from remote URL

The thing you missed (I think) is that xml2js produces some rather … horrible … JS objects - as you will see if you look at the output from your console.log(result). It’s been a while since I looked closely at this, but I do recall that $ appeared somewhere in there. You should look closely at what has been produced and work with that.

To get a clearer view, change to:

console.log(JSON.stringify(result, null, 2));

i understand what do you mean, with

Posts.insert(JSON.stringify(result, null, 2));

I got 2073 fields by key in my collections :sweat:

{ "_id" : "uPP3nFq7o7fJZzbkR", "0" : "{", "1" : "\n", "2" : " ", "3" : " ", "4" : "\"", "5" : "e", "6" : "v", "7" : "e", "8" : "c", "9" : "_", "10" : "a", "11" : "p", "12" : "i", "13" : "\"", "14" : ":", "15" : " ", "16" : "{", "17" : "\n", "18" : " ", "19" : " ", "20" : " ", "21" : " ", "22" : "\"", "23" : "$", "24" : "\"", "25" : ":", "26" : " ", "27" : "{", "28" : "\n", "29" : " ", "30" : " ", "31" : " ", "32" : " ", "33" : " ", "34" : " ", "35" : "\"", "36" : "v", "37" : "e", "38" : "r", "39" : "s", "40" : "i", "41" : "o", "42" : "n", "43" : "\"", "44" : ":", "45" : " ", "46" : "\"", "47" : "2", "48" : ".", "49" : "0", "50" : "\"", "51" : ",", "52" : "\n", "53" : " ", "54" : " ", "55" : " ", "56" : " ", "57" : " ", "58" : " ", "59" : "\"", "60" : "m", "61" : "e", "62" : "t", "63" : "h", "64" : "o", "65" : "d", "66" : "\"", "67" : ":", "68" : " ", "69" : "\"", "70" : "m", "71" : "a", "72" : "r", "73" : "k", "74" : "e", "75" : "t", "76" : "s", "77" : "t", "78" : "a", "79" : "t", "80" : "_", "81" : "x", "82" : "m", "83" : "l", "84" : "\"", to be continue .....

No - I’m suggesting that you use console.log(JSON.stringify(result, null, 2)); to get a better idea of what the converted data looks like before inserting into the collection. So, do something like

try {
var response = HTTP.get("http://api.eve-central.com/api/marketstat?typeid=29668&regionlimit=10000002&hours=12");
console.log(response.content);
var result = xml2js.parseStringSync(response.content);
console.logJSON.stringify(result, null, 2));
//session.subscribe('posts');
//Posts.insert(result);
this.ready();
//return Posts.find();
}

So just log the response and copy/paste back here - don’t insert into the collection just yet.

ok sry my bad,

console.log(JSON.stringify(result, null, 2));

=> App running at: http://localhost:3000/
I20160331-18:41:15.427(2)? {
I20160331-18:41:15.430(2)?   "evec_api": {
I20160331-18:41:15.431(2)?     "$": {
I20160331-18:41:15.432(2)?       "version": "2.0",
I20160331-18:41:15.432(2)?       "method": "marketstat_xml"
I20160331-18:41:15.433(2)?     },
I20160331-18:41:15.433(2)?     "marketstat": [
I20160331-18:41:15.433(2)?       {
I20160331-18:41:15.434(2)?         "type": [
I20160331-18:41:15.434(2)?           {
I20160331-18:41:15.435(2)?             "$": {
I20160331-18:41:15.435(2)?               "id": "29668"
I20160331-18:41:15.437(2)?             },
I20160331-18:41:15.437(2)?             "buy": [
I20160331-18:41:15.438(2)?               {
I20160331-18:41:15.438(2)?                 "volume": [
I20160331-18:41:15.438(2)?                   "1474"
I20160331-18:41:15.439(2)?                 ],
I20160331-18:41:15.439(2)?                 "avg": [
I20160331-18:41:15.440(2)?                   "920928220.51"
I20160331-18:41:15.441(2)?                 ],
I20160331-18:41:15.442(2)?                 "max": [
I20160331-18:41:15.445(2)?                   "1071000000.01"
I20160331-18:41:15.447(2)?                 ],
I20160331-18:41:15.448(2)?                 "min": [
I20160331-18:41:15.449(2)?                   "2300000.00"
I20160331-18:41:15.449(2)?                 ],
I20160331-18:41:15.450(2)?                 "stddev": [
I20160331-18:41:15.450(2)?                   "414107437.77"
I20160331-18:41:15.452(2)?                 ],
I20160331-18:41:15.452(2)?                 "median": [
I20160331-18:41:15.453(2)?                   "1051500000.01"
I20160331-18:41:15.453(2)?                 ],
I20160331-18:41:15.454(2)?                 "percentile": [
I20160331-18:41:15.454(2)?                   "1068717569.59"
I20160331-18:41:15.454(2)?                 ]
I20160331-18:41:15.454(2)?               }
I20160331-18:41:15.455(2)?             ],
I20160331-18:41:15.455(2)?             "sell": [
I20160331-18:41:15.456(2)?               {
I20160331-18:41:15.457(2)?                 "volume": [
I20160331-18:41:15.458(2)?                   "2485"
I20160331-18:41:15.461(2)?                 ],
I20160331-18:41:15.462(2)?                 "avg": [
I20160331-18:41:15.463(2)?                   "1241428744.38"
I20160331-18:41:15.463(2)?                 ],
I20160331-18:41:15.463(2)?                 "max": [
I20160331-18:41:15.463(2)?                   "2010000000.00"
I20160331-18:41:15.464(2)?                 ],
I20160331-18:41:15.464(2)?                 "min": [
I20160331-18:41:15.464(2)?                   "1086300000.02"
I20160331-18:41:15.465(2)?                 ],
I20160331-18:41:15.465(2)?                 "stddev": [
I20160331-18:41:15.465(2)?                   "116355540.29"
I20160331-18:41:15.465(2)?                 ],
I20160331-18:41:15.466(2)?                 "median": [
I20160331-18:41:15.466(2)?                   "1250000000.00"
I20160331-18:41:15.466(2)?                 ],
I20160331-18:41:15.467(2)?                 "percentile": [
I20160331-18:41:15.467(2)?                   "1099672004.46"
I20160331-18:41:15.468(2)?                 ]
I20160331-18:41:15.469(2)?               }
I20160331-18:41:15.470(2)?             ],
I20160331-18:41:15.470(2)?             "all": [
I20160331-18:41:15.471(2)?               {
I20160331-18:41:15.471(2)?                 "volume": [
I20160331-18:41:15.473(2)?                   "224152"
I20160331-18:41:15.473(2)?                 ],
I20160331-18:41:15.474(2)?                 "avg": [
I20160331-18:41:15.475(2)?                   "8584.42"
I20160331-18:41:15.475(2)?                 ],
I20160331-18:41:15.477(2)?                 "max": [
I20160331-18:41:15.478(2)?                   "16597485.32"
I20160331-18:41:15.478(2)?                 ],
I20160331-18:41:15.478(2)?                 "min": [
I20160331-18:41:15.479(2)?                   "0.30"
I20160331-18:41:15.479(2)?                 ],
I20160331-18:41:15.480(2)?                 "stddev": [
I20160331-18:41:15.480(2)?                   "3651679.64"
I20160331-18:41:15.481(2)?                 ],
I20160331-18:41:15.481(2)?                 "median": [
I20160331-18:41:15.481(2)?                   "1.99"
I20160331-18:41:15.482(2)?                 ],
I20160331-18:41:15.482(2)?                 "percentile": [
I20160331-18:41:15.483(2)?                   "1.00"
I20160331-18:41:15.483(2)?                 ]
I20160331-18:41:15.483(2)?               }
I20160331-18:41:15.485(2)?             ]
I20160331-18:41:15.485(2)?           }
I20160331-18:41:15.486(2)?         ]
I20160331-18:41:15.486(2)?       }
I20160331-18:41:15.487(2)?     ]
I20160331-18:41:15.487(2)?   }
I20160331-18:41:15.487(2)? }
I20160331-18:41:17.260(2)? {
I20160331-18:41:17.260(2)?   "evec_api": {
I20160331-18:41:17.261(2)?     "$": {
I20160331-18:41:17.261(2)?       "version": "2.0",
I20160331-18:41:17.261(2)?       "method": "marketstat_xml"
I20160331-18:41:17.261(2)?     },
I20160331-18:41:17.262(2)?     "marketstat": [
I20160331-18:41:17.262(2)?       {
I20160331-18:41:17.262(2)?         "type": [
I20160331-18:41:17.264(2)?           {
I20160331-18:41:17.264(2)?             "$": {
I20160331-18:41:17.264(2)?               "id": "29668"
I20160331-18:41:17.265(2)?             },
I20160331-18:41:17.267(2)?             "buy": [
I20160331-18:41:17.268(2)?               {
I20160331-18:41:17.268(2)?                 "volume": [
I20160331-18:41:17.269(2)?                   "1474"
I20160331-18:41:17.269(2)?                 ],
I20160331-18:41:17.269(2)?                 "avg": [
I20160331-18:41:17.270(2)?                   "920928220.51"
I20160331-18:41:17.271(2)?                 ],
I20160331-18:41:17.271(2)?                 "max": [
I20160331-18:41:17.273(2)?                   "1071000000.01"
I20160331-18:41:17.273(2)?                 ],
I20160331-18:41:17.274(2)?                 "min": [
I20160331-18:41:17.274(2)?                   "2300000.00"
I20160331-18:41:17.275(2)?                 ],
I20160331-18:41:17.277(2)?                 "stddev": [
I20160331-18:41:17.277(2)?                   "414107437.77"
I20160331-18:41:17.278(2)?                 ],
I20160331-18:41:17.280(2)?                 "median": [
I20160331-18:41:17.281(2)?                   "1051500000.01"
I20160331-18:41:17.295(2)?                 ],
I20160331-18:41:17.299(2)?                 "percentile": [
I20160331-18:41:17.301(2)?                   "1068717569.59"
I20160331-18:41:17.304(2)?                 ]
I20160331-18:41:17.308(2)?               }
I20160331-18:41:17.310(2)?             ],
I20160331-18:41:17.311(2)?             "sell": [
I20160331-18:41:17.312(2)?               {
I20160331-18:41:17.312(2)?                 "volume": [
I20160331-18:41:17.312(2)?                   "2485"
I20160331-18:41:17.312(2)?                 ],
I20160331-18:41:17.313(2)?                 "avg": [
I20160331-18:41:17.313(2)?                   "1241428744.38"
I20160331-18:41:17.313(2)?                 ],
I20160331-18:41:17.313(2)?                 "max": [
I20160331-18:41:17.314(2)?                   "2010000000.00"
I20160331-18:41:17.314(2)?                 ],
I20160331-18:41:17.314(2)?                 "min": [
I20160331-18:41:17.315(2)?                   "1086300000.02"
I20160331-18:41:17.315(2)?                 ],
I20160331-18:41:17.315(2)?                 "stddev": [
I20160331-18:41:17.316(2)?                   "116355540.29"
I20160331-18:41:17.319(2)?                 ],
I20160331-18:41:17.320(2)?                 "median": [
I20160331-18:41:17.320(2)?                   "1250000000.00"
I20160331-18:41:17.321(2)?                 ],
I20160331-18:41:17.321(2)?                 "percentile": [
I20160331-18:41:17.322(2)?                   "1099672004.46"
I20160331-18:41:17.322(2)?                 ]
I20160331-18:41:17.322(2)?               }
I20160331-18:41:17.322(2)?             ],
I20160331-18:41:17.324(2)?             "all": [
I20160331-18:41:17.325(2)?               {
I20160331-18:41:17.327(2)?                 "volume": [
I20160331-18:41:17.327(2)?                   "224152"
I20160331-18:41:17.329(2)?                 ],
I20160331-18:41:17.330(2)?                 "avg": [
I20160331-18:41:17.330(2)?                   "8584.42"
I20160331-18:41:17.330(2)?                 ],
I20160331-18:41:17.331(2)?                 "max": [
I20160331-18:41:17.331(2)?                   "16597485.32"
I20160331-18:41:17.332(2)?                 ],
I20160331-18:41:17.332(2)?                 "min": [
I20160331-18:41:17.332(2)?                   "0.30"
I20160331-18:41:17.332(2)?                 ],
I20160331-18:41:17.333(2)?                 "stddev": [
I20160331-18:41:17.333(2)?                   "3651679.64"
I20160331-18:41:17.333(2)?                 ],
I20160331-18:41:17.333(2)?                 "median": [
I20160331-18:41:17.333(2)?                   "1.99"
I20160331-18:41:17.334(2)?                 ],
I20160331-18:41:17.334(2)?                 "percentile": [
I20160331-18:41:17.334(2)?                   "1.00"
I20160331-18:41:17.334(2)?                 ]
I20160331-18:41:17.334(2)?               }
I20160331-18:41:17.335(2)?             ]
I20160331-18:41:17.335(2)?           }
I20160331-18:41:17.335(2)?         ]
I20160331-18:41:17.335(2)?       }
I20160331-18:41:17.335(2)?     ]
I20160331-18:41:17.335(2)?   }
I20160331-18:41:17.337(2)? }