2

I am writing a function of Parse Cloud Code to append a double to an array for a voting system:

Parse.Cloud.define("addVote", function(request){

    Parse.Cloud.useMasterKey();

    var id = request.params.key
    var vote = request.params.vote

    var query = new Parse.Query("Post");
    query.equalTo("objectID", id);
    query.find({
        success: function(poll){
            poll.add("votes", vote)
            poll.save();
        },
        error: function(error){
            console.error("Got an error");
        }
    });
});

I'm calling the function by using:

PFCloud.callFunctionInBackground("addComment", withParameters: ["key" : key, "vote": self.vote)]

I'm getting the response "TypeError: Object has no method 'add'". What am I doing wrong that is causing this error?

4
  • It looks like you're missing a closing ]. Is that just here? Commented Feb 20, 2016 at 0:59
  • What is poll? Where do you define it? Why do you think it does have a method add? Commented Feb 20, 2016 at 1:01
  • To address the comments from @AaronBrager: I was only missing the closing ] in this post. I have updated my question to be more accurate. In the js, I was under the impression that "poll" would be the object returned if the query for a post was successful ("poll" would be a "Post" object which contains an array "votes"). Is this correct? Commented Feb 20, 2016 at 15:04
  • Also, I thought the add method was used to append values to an array in javascript. Is that not true? Commented Feb 20, 2016 at 15:56

2 Answers 2

1

The solution I found:

Parse.Cloud.define("addVote", function(request){

    Parse.Cloud.useMasterKey();

    var id = request.params.key
    var answer = request.params.vote

    var query = new Parse.Query("Post");
    query.get(id, {
        success: function(post){
            post.add("votes", vote);
            post.save();
        },
        error: function(error){
            console.error("Got and error");
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Code below is example for update data users. this code 100% work tested on back4app.com

before using this code, you must declare your variable like below export async function prepareForSending(form) { // available modes: text, numbers, files, pointers and for others you need to

custome some of my code with your needs
  let FORM = [
    {
      mode: "text",
      object: "name",
      value: form.name
    },
    {
      mode: "text",
      object: "phone",
      value: form.phone
    },
    {
      mode: "text",
      object: "address",
      value: form.address
    },
    {
      mode: "text",
      object: "clientStatus",
      value: form.clientStatus.selected
    },
    {
      mode: "pointer",
      object: "role",
      value: {
        __type: "Pointer",
        className: "_Role",
        objectId: form.roles.selected
      }
    }
  ];

  return FORM;
}

and then, now you can use code below

export function ParseUserManualUpdater(ID, DATA) {
  //ID is a users objectId target
  return new Promise(async (resolve, reject) => {
    const params = {
      id: ID,
      data: DATA
    };
    const Cloud = await Parse.Cloud.run("usersUpdater", params);
    if (Cloud == "success") {
      resolve(true);
    } else {
      resolve(false);
    }
  });
}

and upload this code to your code cloud

Parse.Cloud.define("usersUpdater", function(request, response) {
  //  how can I use GET parameters here??
  var ID = request.params.id;
  var DATA = request.params.data;

  const User = new Parse.User();
  const query = new Parse.Query(User);
  query.get(ID).then(user => {
    for (let i = 0; i < DATA.length; i++) {
      let mode = DATA[i].mode;
      let object = DATA[i].object;
      let value = DATA[i].value;
      if (mode == "text") {
        user.set(object, value);
      } else if (mode == "number") {
        user.set(object, Number(value));
      } else if (mode == "pointer") {
        user.set(object, value);
      } else if (mode == "file") {
        user.set(
          object,
          new Parse.File(value.file.name, {
            base64: value.file.base64
          })
        );
      }
    }
    user.save(null, {useMasterKey: true}).then(
      resSucces => {
        response.success("success");
      },
      resError => {
        response.error("error");
      }
    );
  });
});

im hope, my code can help someone there

Comments

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.