2017-06-29 46 views
0

我试图通过MySQL表中的每个帖子来运行,获取用户名,在单独的表中运行单独的查询以从该用户获取信息如配置文件img等,然后把它放在JSON中。NodeJS SQL在for循环中返回'Can not read property'x'undefined'

这项工作,直到我尝试和增加另一个查询用于获取用户信息,然后返回

'Cannot read property 'postId' of undefined' 

我试过很多方法可以解决该,但他们都没有工作。

这里是我的代码:

var postLocation = req.query.postLocation.replace(regex, escaper); 
connection.query("SELECT * FROM `posts` WHERE (postLocation = '" + postLocation + "' OR postLocation = 'Global') AND postDeleted = 'no' ORDER BY postId DESC;", function (err1, rows1, fields1) { 
    if(err1){ 
    res.send(err1); 
    } else if(!rows1.length){ 
    res.send('{"rapids": [{"response": "This post does not exist."}]}'); 
    } else { 
    var queryString = ""; 
    var queryTimes = 0; 
    var profileURL = null; 
    for(i = 0; i < rows1.length; i++){ 
     queryTimes++; 
     connection.query("SELECT * FROM `users` WHERE username = '" + rows1[0].postBy + "' ORDER BY postId DESC;", function (err2, rows2, fields2) { 
     queryString = queryString + '{"postId": "' + rows1[i].postId + '", "post": "' + rows1[i].post + '", "postBy": "' + rows1[i].postBy + '", "postLikes": ' + rows1[i].postLikes + ', "postByProfileImgURL": "' + rows1[i].postByProfileImgURL + '", "postDate": "' + rows1[i].postDate + '", "postViews": ' + rows1[i].postViews + ', "postDeleted": "' + rows1[i].postDeleted + '"'; 
     if(queryTimes == rows1.length){ 
      queryString = queryString + '}'; 
     } else { 
      queryString = queryString + '}, '; 
     } 
     }); 
    } 
    res.send('{"rapids": [' + queryString + ']}'); 
    } 
}); 

谢谢!

+0

碰碰因为 –

回答

0

rows1 [i] .postId在不同的上下文中。你必须把它传递到你的内部查询的运行环境。

var postLocation = req.query.postLocation.replace(regex, escaper); 
     connection.query("SELECT * FROM `posts` WHERE (postLocation = '" + postLocation + "' OR postLocation = 'Global') AND postDeleted = 'no' ORDER BY postId DESC;", function (err1, rows1, fields1) { 
     if(err1){ 
       res.send(err1); 
     } else if(!rows1.length){ 
      res.send('{"rapids": [{"response": "This post does not exist."}]}'); 

     } else { 
      var queryString = "", 
       queryTimes = 0, 
       profileURL = null; 

      for(i = 0; i < rows1.length; i++){ 
       queryTimes++; 

       connection.query("SELECT * FROM `users` WHERE username = '" + rows1[0].postBy + "' ORDER BY postId DESC;", function (rows1, err2, rows2, fields2) { 
        queryString = queryString + '{"postId": "' + rows1[i].postId + '", "post": "' + rows1[i].post + '", "postBy": "' + rows1[i].postBy + '", "postLikes": ' + rows1[i].postLikes + ', "postByProfileImgURL": "' + rows1[i].postByProfileImgURL + '", "postDate": "' + rows1[i].postDate + '", "postViews": ' + rows1[i].postViews + ', "postDeleted": "' + rows1[i].postDeleted + '"'; 

        if(queryTimes == rows1.length){ 
         queryString = queryString + '}'; 

        } else { 
         queryString = queryString + '}, '; 
        } 
       }.bind(this, rows1)); 
      } 

      res.send('{"rapids": [' + queryString + ']}'); 
     } 
    }); 
+0

我会怎么做呢? –

+0

我更新了我的回复。因为我没有使用过mysql,所以我不是100%确定的,但我相信这应该让你更接近。如果可以在内部查询中设置断点,则可以验证回调函数正在使用的参数。 – rkatka

+0

仍然有错误。不知道该怎么办。 –