2014-05-12 68 views
-1

爲什麼我在使用此代碼的Chrome工具中出現「未捕獲的SyntaxError:意外的令牌」其他錯誤,我想我應該在這裏使用else,否則,如果?未捕獲SyntaxError:在JavaScript代碼塊中使用else語句的意外令牌

var friendName; 

function findFriend(){ 
    friendName = $('#friendsearch').val(); 
    console.log(friendName); 

    var query = new Parse.Query(Parse.User); 
    query.equalTo("username", friendName); // find users that match 
query.find({ 
     success: function(friendMatches) { 

      if (friendMatches.length === 0) 
       alert('NO MATCH FOUND!!!'); 
} 
      else { 
      // Query executed with success 

       { alert('MATCH FOUND!!!'); 

     }, 
     error: function (error) { 
     alert('query failed with error' + error.message); 
     } 
}); 
} 

$('#find_button').click(function(e){ 
    findFriend(); 
}); 
+1

爲什麼兩個''{警報之前? – j08691

+0

你能告訴使用錯誤行嗎? – Nano

回答

1

合理(auto-)壓痕,你發現錯誤(S)很容易:

function findFriend() { 
    friendName = $('#friendsearch').val(); 
    console.log(friendName); 
    var query = new Parse.Query(Parse.User); 
    query.equalTo("username", friendName); // find users that match 
    query.find({ 
     success: function (friendMatches) { 
      if (friendMatches.length === 0) 
       alert('NO MATCH FOUND!!!'); 
     } else { 
      // Query executed with success 
      { 
       alert('MATCH FOUND!!!'); 
      }, 
      error: function (error) { 
       alert('query failed with error' + error.message); 
      } 
     }); 
    } 

周圍的大括號else是太多了,應該是

function findFriend() { 
    friendName = $('#friendsearch').val(); 
    console.log(friendName); 
    var query = new Parse.Query(Parse.User); 
    query.equalTo("username", friendName); // find users that match 
    query.find({ 
     success: function (friendMatches) { 
      if (friendMatches.length === 0) 
       alert('NO MATCH FOUND!!!'); 
      else // Query executed with success 
       alert('MATCH FOUND!!!'); 
     }, 
     error: function (error) { 
      alert('query failed with error' + error.message); 
     } 
    }); 
} 

 … 
     success: function (friendMatches) { 
      if (friendMatches.length === 0) { 
       alert('NO MATCH FOUND!!!'); 
      } else { 
       // Query executed with success 
       alert('MATCH FOUND!!!'); 
      } 
     }, 
     … 
+0

感謝您的提示,並花時間幫助我理解。 – Dano007

-1

變化

{ alert('MATCH FOUND!!!'); 

alert('MATCH FOUND!!!'); } 
相關問題