1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

OK Thanks! However I was wondering if it was possible to send an array of Objects in the client.call() (from the java-ddp-client project). I get no answer from the author and I can't find any better Java DDP client than that. I guess I can continue to encode it into just one Object and decode it like you suggest. I was just hoping that there would be a way of sending it so that no manual JSON encoding was necessary. I would like it to be as JS-native as possible.
I think meteor code you wish might be possible, if you encode it properly in Java side. You can try something like this tutorialspoint.com/json/json_java_example.htm and then see how it ends up in Meteor side (depending how ddp client encodes it), maybe there will be no need for JSON parsing. Anyway, if my answer was good enough, do not hesitate to accept it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.