簡體   English   中英

如何通過關系將3個對象從node.js保存到parse.com

[英]How to save 3 objects from node.js to parse.com with relations

我正在嘗試使用node.js(v0.10.28,mac os x)將3個對象保存到parse.com。 這些對象之間有關系。

關系如下: 數據之間的關系

我使用數據瀏覽器配置了類:

  • 汽車(未通過腳本更新) 在此處輸入圖片說明
  • 驅動器 在此處輸入圖片說明
  • 水庫 在此處輸入圖片說明
  • 斯塔斯托 在此處輸入圖片說明

我的目標是了解關系(是的,我閱讀了parse.com上有關關系的文檔,但不能將其應用於此簡單示例)。 我的腳本會

  1. 查詢我想要的汽車(parse.com中只有1個),
  2. 然后它將為CarDrivesRes和`StaSto創建對象
  3. 然后它將關聯對象並在其中放置一些假值。
  4. 最后,它將嘗試保存它們。 這是腳本因錯誤而Failed to create new myRes, with error code: undefined地方: Failed to create new myRes, with error code: undefined myDrive被創建,即使它應該是要創建的最后一個元素,並創造myRes失敗。

有人可以幫忙嗎? 還是有其他更好的方法呢?

謝謝。

這是我的腳本:

console.log("Starting");
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PARSE Classes!
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var Parse = require('parse').Parse;
var parse_JSKey = "xxx";
var parse_AppKey = "xxx";
Parse.initialize(parse_AppKey, parse_JSKey);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PARSE Classes!
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var Drives = Parse.Object.extend("Drives");
var StaSto = Parse.Object.extend("StaSto");
var Res = Parse.Object.extend("Res");
var Car = Parse.Object.extend("Car");
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// fake data
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var myDriveData = {'distance':21, 'speed': 99};
var carID = "mrNQcPRNl4"; //already in parse.com!
var myStaSto_lat = 48.1333;
var myStaSto_lng = 11.5667;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// insert into parse
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//   --- query for the car
var carQuery = new Parse.Query(Car);
carQuery.get(carID, {
  success: function(result) {
    var thisCar = result;
//   --- create a new drive
    var myDrive = new Drives(); // created a new object, but empty
    myDrive.set('distance', myDriveData.distance);
    myDrive.set('speed', myDriveData.speed);
//   --- create a new StaSto
    var myStaSto = new StaSto();
    myStaSto.set('coord',new Parse.GeoPoint({latitude: myStaSto_lat, longitude: myStaSto_lng}));
    myStaSto.set('drive',myDrive);
//   --- create a new Res
    var myRes = new Res();
    myRes.set('drive',myDrive);
    myRes.set('car',thisCar);
    myRes.set('res',717);
    console.log(myRes);
//   --- ----> save them all
    myRes.save(null, {
      success: function(response) {
        // Execute any logic that should take place after the object is saved.
        console.log('saved myRes succesfully');
        myStaSto.save(null,{
          success: function(response){
            console.log('saved myStaSto succesfully');

            myDrive.save(null,{
              success: function(response){
                console.log('saved myDrive succesfully');

              },
              error: function(response,error){
                  console.log('Failed to create new myDrive, with error code: ' + error.description);
              }
            }); // end of myDrive.save

          },
          error: function(response,error){
              console.log('Failed to create new myStaSto, with error code: ' + error.description);
          }
        }); // end of myStaSto save
      },
      error: function(response, error) {
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and description.
        console.log('Failed to create new myRes, with error code: ' + error.description);
      }
    }); // end of myRes.save
  },
  error: function(object, error) {
    console.log('error in car query: ' + error.code + " " + error.message);
  }
}); // end of car query.



// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

不要在'save.res周圍的代碼中看到任何錯誤。 您可能想閱讀有關解析承諾的內容,以便...

嘗試以不太復雜的形式使用它。

嘗試僅使用一個FK類型關系保存'res',並在'res.drive'中說一個直值。

如果您有幸得到一個不錯的Save而不是'undef'錯誤的信息,那么...

您可以嘗試將您當前的一種關系類型切換為指針,看看是否可行。

原來我的代碼是正確的。 出現錯誤是因為我在parse.com中將列設置為關系(參見圖片)。

我將其更改為Pointer,瞧,它工作得很好。 我必須承認parse.com的JavaScript開發人員指南在提及這一點時有點短。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM