これがすでに存在するトピックに関するものである場合は申し訳ありませんが、私の問題を実際に検索する必要があるかどうかはよくわかりません。
関数内の Parse.com データベースから関数 getHighestObjectId で Id を受信しようとしています。
Einstellen.js:
EinstellenStart.Einstellen = (function() {
var that = {},
anbieterId = null,
vermieterId = null,
objektId = null,
writeObjekt = function(objekt, o_id, a_id, v_id) {
console.log(objekt.ort);
var objektData = Parse.Object.extend(objekt.ort);
var newObjekt = new objektData();
newObjekt.set("Strasse", objekt.strasse);
newObjekt.set("Hausnummer", parseInt(objekt.hausnummer));
newObjekt.set("PLZ", parseInt(objekt.plz));
//newObjekt.set("Ort", objekt.ort);
newObjekt.set("ID", o_id);
newObjekt.set("Einsteller_id", a_id);
newObjekt.set("Vermieter_id", v_id);
newObjekt.set("Miete", parseInt(objekt.miete));
newObjekt.set("Nebenkosten", parseInt(objekt.nebenkosten));
newObjekt.set("Kaution", parseInt(objekt.kaution));
newObjekt.set("Flaeche", parseInt(objekt.flaeche));
newObjekt.set("Zimmer", parseInt(objekt.zimmer));
newObjekt.set("Freitext", objekt.freitext);
newObjekt.set("Gesamtmiete", parseInt(objekt.miete+objekt.nebenkosten));
newObjekt.save(null, {
success: function(newObjekt) {
console.log("Successfully created " + newObjekt);
},
error: function(newObjekt, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to create new object, with error code: ' + error.description);
}
});
},
writeAnbieter = function(anbieter, a_id) {
var newAnbieter = new anbieterData();
newAnbieter.set("Vorname", anbieter.vorname.toString());
newAnbieter.set("Nachname", anbieter.nachname.toString());
newAnbieter.set("Strasse", anbieter.strasse.toString());
newAnbieter.set("Hausnummer", parseInt(anbieter.hausnummer));
newAnbieter.set("PLZ", parseInt(anbieter.plz));
newAnbieter.set("Ort", anbieter.ort.toString());
newAnbieter.set("Telefon", parseInt(anbieter.telefon));
newAnbieter.set("Email", anbieter.email.toString())
newAnbieter.set("ID", a_id);
newAnbieter.save(null, {
success: function(newAnbieter) {
},
error: function(newAnbieter, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to create new object, with error code: ' + error.description);
}
});
},
writeVermieter = function(vermieter, a_id) {
var newVermieter = new vermieterData();
newVermieter.set("Vorname", vermieter.vorname.toString());
newVermieter.set("Nachname", vermieter.nachname.toString());
newVermieter.set("Strasse", vermieter.strasse.toString());
newVermieter.set("Hausnummer", parseInt(vermieter.hausnummer));
newVermieter.set("PLZ", parseInt(vermieter.plz));
newVermieter.set("Ort", vermieter.ort.toString());
newVermieter.set("Telefon", parseInt(vermieter.telefon));
newVermieter.set("Email", vermieter.email.toString())
newVermieter.set("ID", a_id);
newVermieter.save(null, {
success: function(newVermieter) {
},
error: function(newVermieter, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and description.
alert('Failed to create new object, with error code: ' + error.description);
}
});
},
getHighestObjektId = function(objekt, callback) {
var objektData = Parse.Object.extend(objekt);
var id__objekt_query = new Parse.Query(objektData);
id__objekt_query.exists("ID");
id__objekt_query.descending("ID");
id__objekt_query.first({
success: function(object) {
callback(parseInt(object.get('ID')) + 1);
//objektId = parseInt(object.get('ID')) + 1;
console.log(objektId);
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
console.log(objektId);
return objektId;
},
getHighestAnbieterId = function() {
var id__anbieter_query = new Parse.Query(anbieterData);
id__anbieter_query.exists("ID");
id__anbieter_query.descending("ID");
id__anbieter_query.first({
success: function(object) {
anbieterId = parseInt(object.get('ID')) + 1;
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
console.log("anbieter:" + anbieterId);
return anbieterId;
},
getHighestVermieterId = function() {
var id_vermieter_query = new Parse.Query(vermieterData);
id_vermieter_query.exists("ID");
id_vermieter_query.descending("ID");
id_vermieter_query.first({
success: function(object) {
vermieterId = parseInt(object.get('ID')) + 1;
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
return vermieterId;
},
setupParse = function() {
Parse.initialize("REsOHm341bAXyLksjPxBq0AX0wfHs2DZVvZsjEEF", "9uQb8PLO8UZAG8rzWMxL0vGYdfaEd3zru6it73bS");
anbieterData = Parse.Object.extend("Anbieter");
vermieterData = Parse.Object.extend("Vermieter");
},
init = function() {
console.log("init Einstellen.js")
setupParse();
getHighestAnbieterId();
getHighestVermieterId();
return that;
};
that.getHighestObjektId = getHighestObjektId;
that.getHighestAnbieterId = getHighestAnbieterId;
that.getHighestVermieterId = getHighestVermieterId;
that.writeObjekt = writeObjekt;
that.writeVermieter = writeVermieter;
that.writeAnbieter = writeAnbieter;
that.init = init;
return that;
})();
別のスクリプトから関数を呼び出しました。
EinstellenStart.EinstellenController = (function() {
var that = {},
einstellen = null,
einstellenView = null,
objektId = null,
o_id = null,
a_id = null,
v_id = null,
stadt = null,
init = function() {
einstellen = EinstellenStart.Einstellen.init();
einstellenView = EinstellenStart.EinstellenView.init();
$(einstellenView).on("setNewObject", onSetNewObject);
},
onSetNewObject = function(event) {
v_id = einstellen.getHighestVermieterId();
a_id = einstellen.getHighestAnbieterId();
var anbieter = einstellenView.getAnbieterFormData();
var vermieter = einstellenView.getVermieterFormData();
var objekt = einstellenView.getObjektFormData();
einstellen.getHighestObjektId(objekt.ort, function(o_id) {
o_id = objektId;
console.log(o_id + "onSetNewObject");
});
einstellen.writeAnbieter(anbieter, a_id);
einstellen.writeVermieter(vermieter, v_id);
einstellen.writeObjekt(objekt, o_id, a_id, v_id);
};
that.init = init;
return that;
})();
今私の問題。値 objektId は正しく受信されます。query.first 関数内のログ出力は、コンソールに正しい値を返します。
しかし、2 番目のログは null を返し、関数全体も返します。関数でパブリック変数が変更された理由がわかりませんが、その後再びnullになります。
何か案が?