如果这是一个已经存在的主题,请原谅我,但我真的不知道我应该真正搜索我的问题。
我尝试从函数中的 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 函数中的日志输出在控制台中返回正确的值。
但是第二个日志也返回 null 和整个函数。我只是不明白为什么公共变量在函数中被更改,但之后又为 null。
任何想法?