I'm trying to use node.js (v0.10.28, mac os x) to save 3 objects to parse.com. The objects have relations to each other.
Here are the relations:
I configured the Classes using the data browser:
- Car (not being updated by the script)
- Drives
- Res
- StaSto
My goal is to understand Relations (yes, I read the documentation on relations on parse.com, but I can't apply it to this simple example). My script will
- query for the car I want (only 1 in parse.com),
- then it will create the objects for
Car
,Drives
,Res
and `StaSto - then it will relate the objects and put some fake values in them.
- and finally, it will try to save them. Here is where the script fails with the error:
Failed to create new myRes, with error code: undefined
.myDrive
gets created, even if it should be the last element to be created and the creation ofmyRes
fails.
can anyone helps? Or is there other better way to do it?
Thanks.
Here is my script:
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.
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++