在解析云代码中将数据附加到数组

时间:2016-02-20 00:33:45

标签: javascript arrays swift parse-platform cloud

我正在编写Parse Cloud Code的函数,为投票系统的数组附加一个double:

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");
        }
    });
});

我使用以下方法调用该函数:

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

我收到回复" TypeError:对象没有方法'添加'"。我做错了什么导致了这个错误?

2 个答案:

答案 0 :(得分:0)

我发现的解决方案:

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");
        }
    });
});

答案 1 :(得分:0)

下面的代码是更新数据用户的示例。此代码100%在back4app.com上进行了测试

在使用此代码之前,必须像下面这样声明变量 导出异步函数prepareForSending(form){   //可用模式:文本,数字,文件,指针,对于其他模式,您需要

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;
}

然后,您现在可以使用下面的代码

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);
    }
  });
}

并将此代码上传到您的代码云

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");
      }
    );
  });
});

希望我的代码可以帮助那里的人