2015-10-06 17 views
0

我有一個用戶類,包含兩種類型的用戶 - 客戶和供應商。用戶表中有一個名爲vendorLocation的列(GeoPoint),它具有供應商商店的座標,但對於客戶保留爲空(空)。在解析的雲代碼中使用地理位置

當客戶下訂單時,會在Order Class中創建一個新對象,我將地址位置存儲爲GeoPoint。我想找到離他最近的供應商。我試圖寫一個雲代碼,但一直面臨同樣的錯誤。

Parse.Cloud.define("assignVendor", function(request, response){ 
    var orderObjectId = request.params.orderObjectId; 
    var query= new Parse.Query("Order") 
    query.equalTo("objectId", orderObjectId); 
    query.first({ 
     success: function(order){ 
     //order is the newly created parse order object 
     //console.log("Order object found"+ order.get("orderNumber")); 
     var userGeoPoint=new Parse.GeoPoint(); 
     //This is the problematic line 
     userGeoPoint = order.get("customerLocation"); 
     var Query = new Parse.Query(Parse.User); 
     Query.exists("vendorLocation"); 
     Query.near("vendorLocation", userGeoPoint); 
     console.log("Reached here"); 
     Query.find({ 
      success: function(results){ 
       var chemist= results[0]; 
        response.success("found "+vendor.get("profileName")); 
        order.put("vendorLocation", chemist); 
         order.save(null, { 
          success: function(result) {response.success("Saved")}, 
          error: function(error) {console.log("Failed at save")} }); 
      }, 
      error:function(error){console.log("Cant find a suitable vendor")} 
     }); 

    }, error:function(error){console.log("cant find the ");} 

    }); 
}); 

雲代碼控制檯中顯示的錯誤。

E2015-10-06T11:23:40.858Z]v13 Ran cloud function assignVendor for user 4otr3l7YwG with: 
    Input: {"orderObjectId":"OS5siGRXYW"} 
    Result: TypeError: Cannot call method 'get' of undefined 
    at e.Query.find.success (main.js:25:53) 
    at e.<anonymous> (Parse.js:14:27927) 
    at e.s (Parse.js:14:26859) 
    at e.n.value (Parse.js:14:26278) 
    at e.s (Parse.js:14:26987) 
    at e.n.value (Parse.js:14:26278) 
    at e.s (Parse.js:14:26987) 
    at e.n.value (Parse.js:14:26278) 
    at e.<anonymous> (Parse.js:14:26931) 
    at e.s (Parse.js:14:26859) 

I2015-10-06T11:23:40.924Z]Reached here 

了Android代碼的Android代碼

 HashMap atMap=new HashMap<>(); 
     atMap.put("orderObjectId", "OS5siGRXYW"); 
     ParseCloud.callFunctionInBackground("assignVendor", atMap, new FunctionCallback<String>() { 
      @Override 
      public void done(String value, ParseException e) { 
       if (e!=null){Log.d("failed ", +e.getCode()+e.getMessage());} else {Log.d("s", value);} 
      } 
     }); 

錯誤顯示是完全一樣的控制檯上,所以我havnt再次張貼。錯誤編號是'141'

回答

0

這裏有一些幫助可以追蹤問題的原因。

  1. 確保訂單號碼正確傳遞 並且存在您傳遞的對象ID的順序。
  2. 當直接將嵌套回調 分配給first()函數時,您可能會遇到問題。最好使用then()將承諾串起來。
  3. 最好是使用get()不是試圖 當找到一個對象使用first(),你應該得到一個object not found錯誤 如果get()回報什麼,防止cannot call method get 錯誤。

讓我們來收拾你的代碼,看看是否有幫助:

Parse.Cloud.define("assignVendor", function(request, response){ 
    var fetchedOrder; 
    var orderObjectId = request.params.orderObjectId; 
    console.log("orderObjectId = " + orderObjectId); 
    var orderQuery= new Parse.Query("Order") 
    orderQuery.get(orderObjectId) 
     .then(function(order){ 
      fetchedOrder = order; 
      console.log("Order object found"+ fetchedOrder.get("orderNumber")); 
      userGeoPoint = fetchedOrder.get("customerLocation"); 
      var userQuery = new Parse.Query(Parse.User); 
      userQuery.near("vendorLocation", userGeoPoint); 
      return userQuery.first(); 
     }) 
     .then(function(chemist){ 
      order.put("vendorLocation", chemist); 
      return order.save(); 
     }) 
     .then(function(){ 
       response.success("Success!"); 
     }, function (error){ 
      console.error(JSON.stringify(error)); 
      response.error(error); 
     }); 
}); 

注:

  • 你不需要得到 customerLocation之前定義一個新的Parse.Geopoint。你只是在那裏重新分配一個變量。
  • 您不需要檢查vendorLocation是否在 之前存在,並將其與userGeoPoint進行比較。如果它不存在,它只是 不會得到使用near
  • 時,我只會在最後所有 承諾線程使用response.success勸返回
  • 後userQuery.find(),我認爲,爲了不存在在那個 上下文中,所以我將它設置爲一個函數變量。
  • 無需使用results[0],只需使用first()