Peter Andersson
Peter Andersson

Reputation: 2047

Adding data to Meteor from Java

I'm using Java DDPClient (https://github.com/kutrumbo/java-ddp-client) and I am trying to insert data into a meteor application.

I do this from java:

    DdpClient client;
    try {
        client = new DdpClient("localhost", 3000);
        client.addObserver(this);
        client.connect();
        Object[] objArray = new Object[1];
        objArray[0] = new String("{name:'peter andersson', phone:'12345678'}");
        client.call("createNewCustomer", objArray);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        System.out.println("Exception:" + ex.getLocalizedMessage());
    }

and this in Meteor (collection Customers)

Meteor.methods({
    "createNewCustomer" : function(options) {
      var ret = {};
      options.replace(/(\b[^:]+):'([^']+)'/g, function ($0, param, value)      {
      ret[param] = value;
    });
    Customers.insert(ret);
  }
});

It works, however it seems like unnecessary work to code it into a string and then decode it to a javascript hashmap.

I have tried creating an array of Objects (Strings) but no matter how I do it it doesnt work as expected.

What's the "proper" way of doing it?

EDIT:

My wish is that the Meteor code would look like this:

Meteor.methods({
    "createNewCustomer" : function(options) {
      Customers.insert(options);
    }
});

I guess what I want to know is how to send from Java (using Java DDP Client) so that no decoding is necessary in Meteor.

Upvotes: 1

Views: 366

Answers (1)

Martins Untals
Martins Untals

Reputation: 2288

Proper way would be to decode the string using JSON.parse(). Just make sure that string is valid JSON document.

in your java code change it to:

objArray[0] = new String("{\"name\":\"peter andersson\", \"phone\":\"12345678\"}");

and in your javascript code should now look like this:

Meteor.methods({
  "createNewCustomer" : function(options) {
    var ret = JSON.parse(options);             
    Customers.insert(ret);
  }
});

p.s. Also in your java code you might look into possibility to construct JSON string using some JSON library, if you want to do it dynamically.

Upvotes: 1

Related Questions