dennismonsewicz
dennismonsewicz

Reputation: 25542

MeteorJS: Having a hard time understanding collections

I am a newb to MeteorJS and I am building an app that plots points on a Google Map (using their API) based on an XML api service and when clicking on one of those points, detailed information will be displayed. Pretty straight forward.

What I am struggling with is that I need to store the API results so that the API isn't pinged every time the page is loaded and since Meteor uses MongoDB, I thought of storing the results in a collection, but I am just not sure I understand how to use them.

Here is what a the XML feed will look like:

<api version="1.0">
    <id>597837338</id>
    <time>3/6/2014 11:46:46 PM</time>
    <machine>query://djsearch6/dejobs</machine>
    <query>sales OR marketing</query>
    <recordcount>500</recordcount>
    <startrow>1</startrow>
    <endrow>10</endrow>
    <order>relevance</order>
    <jobs>
    <job>
        <title>
        Customer Service Representative - State Farm Agent Team Member (Property and Casualty Insurance Focus)
        </title>
        <url>http://my.jobs/a897284496c14249a8473d5947d08b4d321</url>
        <company>Cie Taylor - State Farm Agent</company>
        <location>Mesa, AZ</location>
        <dateacquired>2014-1-19 1:31 AM</dateacquired>
        <jvid>a897284496c14249a8473d5947d08b4d321</jvid>
    </job>
    <job>
        <title>
        Insurance and Financial Services Position - State Farm Agent Team Member (Sales experience preferred)
        </title>
        <url>http://my.jobs/1b717b9492464fbabbd22d3c0e8cf08d321</url>
        <company>Cie Taylor - State Farm Agent</company>
        <location>Mesa, AZ</location>
        <dateacquired>2014-1-19 1:31 AM</dateacquired>
        <jvid>1b717b9492464fbabbd22d3c0e8cf08d321</jvid>
    </job>
  </jobs>
</api>

Upvotes: 0

Views: 79

Answers (1)

lfergon
lfergon

Reputation: 973

I think this could give you a general idea, if I don´t have understand your question in the correct way, my apologies:

For parsing from XML to JSON: http://davidwalsh.name/convert-xml-json

First parse the XML to a JSON, for example imagine this JSON extracted from XML:

var dataFromXML = {
    "company": 597837338,
    "createdAt": "3/6/2014 11:46:46 PM",
    "jobs": [
        {"title": "job1", "url": "job1url", "company": "job1company", "location": "job1location", "dateUpdate": "job1date", "id": "job1id" },
        {"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
    ]
};

Now on a server script you can insert this data on a collection:

JobsCollection.insert({company: dataFromXML.company ,dataFromAPI: dataFromXML});

In case your result is an array you can run this on a forEach loop:

var dataFromXMLArray = [
    {
        "company": 597837338,
        "createdAt": "3/6/2014 11:46:46 PM",
        "jobs": [
            {"title": "job1", "url": "job1url", "company": "job1company", "location":   "job1location", "dateUpdate": "job1date", "id": "job1id" },
            {"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
        ]
    },
    {
        "company": 597837338,
        "createdAt": "3/6/2014 11:46:46 PM",
        "jobs": [
            {"title": "job1", "url": "job1url", "company": "job1company", "location": "job1location", "dateUpdate": "job1date", "id": "job1id" },
            {"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
        ]
    }
];

dataFromXMLArray.forEach(function (data) {
    JobsCollection.insert({company: data.company, dataFromAPI: data});
});

Upvotes: 1

Related Questions