1

excuse me if this is on a Topic that already exists, but I don't really know what I should really search for my problem.

I try to receive a Id in the function getHighestObjectId from a Parse.com Database within a Function:

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;
})();

I called the function from another script:

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;
})();

Now my problem. The value objektId is received correctly. The log output within the query.first function returns the correct value in the console.

But the second log returns null and the whole function too. I just don't get why the public variable is changed in the function but afterwards null again.

Any Idea?

Igl3
  • 4,900
  • 5
  • 35
  • 69

1 Answers1

1

That is because of the async nature of the context you're dealing with, the call is finished in the success callback, the point is that when the second console.log is called the call might not have finished, so, even if you declared objektId at the topmost level, you would still not get what you expected, but the value you initialized it to.

One possible answer could be

getHighestObjektId = function(objekt, callback) {
  // your code...

    success: function(object) {
      callback(parseInt(object.get('ID')) + 1);
    }

  // rest of your code

which you will call like this

getHighestObjektId(objekt, function(objektId) {
  // here you can deal with objektId
});
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • Okay I added this, now both console outputs are null :D – Igl3 Aug 19 '13 at 14:44
  • I posted the whole Scripts. I think I just got something wrong with the callback thing. Another interesting point is, that the other functions getAnbieterId and getVermieterId return the values properly :/ – Igl3 Aug 19 '13 at 14:45
  • Make sure that the call is returning with the correct data at all. – Alberto Zaccagni Aug 19 '13 at 14:51
  • How? I am logging in the call: einstellen.getHighestObjektId(objekt.ort, function(o_id) { o_id = objektId; console.log(objektId); }); and it says null – Igl3 Aug 19 '13 at 14:55
  • Before calling the callback, in your success function, so that you're sure that you're actually receiving something from the call. – Alberto Zaccagni Aug 19 '13 at 15:00
  • So i do the call within the success function? – Igl3 Aug 19 '13 at 15:04
  • Generally speaking using a callback means: "I do not wait for you, async call, to finish your work, here is a callback, call it with the result of your operation when you're done". So yes, call the callback when you have the value, ie in the success function. – Alberto Zaccagni Aug 19 '13 at 15:09
  • Okay, i called it in there and got a infinite loop. And the first time it still returns null, and then always the right id: null EinstellenController.js:35 null Einstellen.js:119 null Einstellen.js:134 Successfully created [object Object] Einstellen.js:33 7 Einstellen.js:119 7 Einstellen.js:134 – Igl3 Aug 19 '13 at 15:45
  • Make one step at a time, first make sure that your call is returning something. Once you have that use the callback as I've written. You should be able to solve your problem this way, if you're having an infinite loop make sure that 1) you are calling the callback only once in the `success` function and 2) you are not calling again the function to get the id from it. – Alberto Zaccagni Aug 19 '13 at 15:53