如何使用关系将node.js中的3个对象保存到parse.com

时间:2014-06-10 00:17:05

标签: javascript node.js parse-platform relationship

我正在尝试使用node.js(v0.10.28,mac os x)将3个对象保存到parse.com。对象彼此有关系。

以下是关系: relations between data

我使用数据浏览器配置了类:

  • 汽车(未通过脚本更新) enter image description here
  • 驱动器 enter image description here
  • RES enter image description here
  • STASTO enter image description here

我的目标是理解关系(是的,我在parse.com上阅读关于关系的文档,但我不能将它应用于这个简单的例子)。我的剧本将

  1. 查询我想要的车(在parse.com上只有1个),
  2. 然后它会为CarDrivesRes和`StaSto
  3. 创建对象
  4. 然后它将关联对象并在其中放入一些假值。
  5. 最后,它会尝试保存它们。以下是脚本失败的错误:Failed to create new myRes, with error code: undefinedmyDrive被创建,即使它应该是要创建的最后一个元素,并且myRes的创建失败。
  6. 任何人都可以帮忙吗?或者还有其他更好的方法吗?

    感谢。

    这是我的剧本:

    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.
    
    
    
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    

2 个答案:

答案 0 :(得分:0)

不要在'save.res周围看到你的代码有什么问题。您可能希望阅读parse promises tho ...

尝试以不太复杂的形式玩它。

尝试使用一个FK类型关系保存'res',并在'res.drive'中说一个直值。

幸运的是,你得到一个很好的保存而不是'undef'错误然后......

您可以尝试将当前关系类型之一切换为指针,看看是否有效。

答案 1 :(得分:0)

原来我的代码是正确的。出现错误是因为我在parse.com中将列设置为关系(参见图片)。

我把它改成了指针,瞧,它运作得很好。我必须承认,parse.com的JavaScript开发人员指南有点简短。

enter image description here