简体   繁体   中英

getting the last / newest object from Parse.com

What I have is a input for a name and a input button to save that name to the Parse database, also on the load event i pull names from the database and present them to the user.

what i'm trying to do is: when the page is loaded a list of all the names are presented to the user. and when the button is clicked it will add that user to the list and appear when the page is refreshed.

This live example is basically what I want just without "thomas" being added over and over again in this case.

I know I used query.first but query.find would only save the name to the database but not display it. So i left it in so as to help demonstrate what i'm trying to achieve.

I figured it out with help from This Question and below is the working code.

$(window).load(function () {

        var Contact = Parse.Object.extend("Contact");
        var query = new Parse.Query(Contact);

        query.equalTo("objectId");
        query.find({
            success: function (results) {

                for (var i = 0; i < results.length; i++) {

                    var firstname = document.createElement("label");

                    firstname.innerHTML = (results[i].attributes.FirstName.toString());

                    document.getElementById("content").appendChild(firstname);

                    var delBtn = document.createElement("button");

                    delBtn.innerHTML = ("Delete");

                    delBtn.onclick = function () {
                        alert("button onclick");
                    }

                    document.getElementById("content").appendChild(delBtn);

                    var br = document.createElement("br");

                    document.getElementById("content").appendChild(br);

                }

            },
            error: function (error) {
                ("Error: " + error.code + " " + error.message);
            }
        });
    });

and the below code will just find the last name

$(window).load(function () {

        var Contact = Parse.Object.extend("Contact");
        var query = new Parse.Query(Contact);

        query.equalTo("objectId");
        query.find({
            success: function (results) {

                alert(JSON.stringify(results));
                for (var i = 0; i < results.length; i++) {
                    content.innerHTML = ((results[i].attributes.FirstName.toString()));                      

                }

            },
            error: function (error) {
                ("Error: " + error.code + " " + error.message);
            }
        });

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM