简体   繁体   中英

Parse.com wait until async is done trouble

I am new to the javascript part of parse.com and I am trying to first find some records and then do something else but have no luck so far :-/

Here is what i am trying to do:

var Comments = Parse.Object.extend("GroupWallComments");
var query1 = new Parse.Query(Comments);
query1.equalTo("story", story.id);
query1.find().then(function(results2){

    for (var i = 0; i < results2.length; i++) {
            var comment = results2[i];

            $("#story"+i+"").append('<div class="comment media"><div class="media-left"><a class="avatar avatar-lg" href="javascript:void(0)"><img src="global/portraits/6.jpg" alt="..."></a></div>'+
        '<div class="comment-body media-body"><a class="comment-author" href="javascript:void(0)">'+comment.get("username")+'</a>'+
        '<div class="comment-meta"><span class="date">Just now</span></div>'+
        '<div class="comment-content"><p>'+comment.get("comment")+'</p></div>'+
        '</div></div>');

    }

}).then(function(commentForm){
    $("#story"+i+"").append('<form class="comment-reply" action="#" method="post">'+
            '<div class="form-group"><textarea class="form-control" rows="5" placeholder="Comment here"></textarea></div>'+
            '<div class="form-group"><button type="submit" class="btn btn-primary">Comment</button></div>'+
            '</form>');
});

Now, the first part comes out as it should, but the last "then" do not seem to work :-/ I do not get the form at the end of the comments. I can not see what i am doing wrong.

Any help is appreciated and thanks in advance :-)

It doesn't seem tricky at all. The last thing appended is a constant string, so...

query1.find().then(function(results2){

    for (var i = 0; i < results2.length; i++) {
            var comment = results2[i];

            $("#story"+i+"").append('<div class="comment media"><div class="media-left"><a class="avatar avatar-lg" href="javascript:void(0)"><img src="global/portraits/6.jpg" alt="..."></a></div>'+
        '<div class="comment-body media-body"><a class="comment-author" href="javascript:void(0)">'+comment.get("username")+'</a>'+
        '<div class="comment-meta"><span class="date">Just now</span></div>'+
        '<div class="comment-content"><p>'+comment.get("comment")+'</p></div>'+
        '</div></div>');

    }
    $("#story"+i+"").append('<form class="comment-reply" action="#" method="post">'+
            '<div class="form-group"><textarea class="form-control" rows="5" placeholder="Comment here"></textarea></div>'+
            '<div class="form-group"><button type="submit" class="btn btn-primary">Comment</button></div>'+
            '</form>');
});
// no then() here, there's nothing left to do

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