lschlessinger
lschlessinger

Reputation: 1954

How to send array of data to Parse Cloud Code?

I'm trying to send an array of contact emails (strings) in the Parse cloud code function parameter. I'm doing it like this:

HashMap<String, ArrayList<String>> params = new HashMap<>();
ArrayList<String> array = new ArrayList<>();
array.add("contact email 1");
array.add("contact email 2");
array.add("contact email 3");

params.put("contacts", array);

ParseCloud.callFunctionInBackground("cloudFunctionName", params, new FunctionCallback<Object>() {
    @Override
    public void done(Object o, ParseException e) {
        // do something
    }
});

I define my cloud function here: contacts should be something like: {"contacts" : ["contact email 1", "contact email 2", "contact email 3"]}. I iterate of each of the emails and perform some logic with each.

var _ = require("underscore");

Parse.Cloud.define("cloudFunctionName", function (request, response) {
var contacts = request.params.contacts;

_.each(contacts, function(contactEmail) {
    var userQuery = Parse.Query(Parse.User);

    userQuery.equalTo("email", contactEmail);

    userQuery.first().then(
        function(user) {
            // there is a user with that email
        },
        function(error) {
            // no user found with that email
});

});

The problem I'm getting is that sometimes contactEmail is undefined.

I get the error: Result: TypeError: Cannot call method 'equalTo' of undefined on the line userQuery.equalTo("email", contactEmail);

Even if I write if(typeof contactEmail != "undefined") { userQuery.equalTo("email", contactEmail); }, I still get the error.

I also check that the emailString is not empty before adding it to the array?

How can I fix this?

Upvotes: 1

Views: 2574

Answers (1)

Fosco
Fosco

Reputation: 38526

Update your JavaScript to create the Users query like this:

var userQuery = new Parse.Query(Parse.User);

Upvotes: 1

Related Questions