在Parse.Cloud中关联文件

时间:2014-07-04 12:05:21

标签: parse-platform

我是通过REST API上传图片并获得如下答案

{
  "url": "http://files.parsetfss.com/346a0978-68c7-4d08-a446-62f7422469e7/tfss-8b131ff0-5fd0-4dce-92e8-b7b94da5db9e-pic.jpg",
  "name": "tfss-8b131ff0-5fd0-4dce-92e8-b7b94da5db9e-pic.jpg"
}

我想将此图片与Promotion对象相关联。我还有一个Location对象,其中包含Promotion个数组。这是我的代码:

function promiseToAddPromotionToLocation (locationID, promotion) {
  var query = new Parse.Query("Location");
  return query.get(locationID).then(function (location) {
    var promotionObject = promotionObjectFromJSON(promotion);
    location.add("promotions", promotionObject);
    return location.save();
  }, function (error) {
    return Parse.Promise.error(error);
  });
}

function promotionObjectFromJSON (promotion) {
  var Promotion = Parse.Object.extend("Promotion");
  var promotionObject = new Promotion();

  if ("message" in promotion) {
    promotionObject.set("message", promotion.message);
  }

  //This causes an error: Uncaught Tried to save an object containing an unsaved file.
  if ("photo") {
    promotionObject.set("photo", promotion.photo);
  }

  return promotionObject;
}

当我注释掉设置照片的部分时,它会正确保存promotion,但是当我尝试设置文件时,它会显示错误Uncaught Tried to save an object containing an unsaved file.如何解决此问题?

顺便使用以下参数调用promiseToAddPromotionToLocation

{
  "locationID": "fvOiAsoogc", 
  "promotion":{
    "message":"Some text", 
    "photo":{"name": "tfss-8b131ff0-5fd0-4dce-92e8-b7b94da5db9e-pic.jpg", "__type": "File"} 
  }
}

1 个答案:

答案 0 :(得分:1)

原因是我没有网址传递文件信息。 Associating file docs表明它只需要nametype。但是,这是错误的。它还需要urlThis question in Parse forum揭示了它。