目前我已经使用NodeJS在Heroku上部署了我的网站。
我遇到了一个奇怪的问题:一些SQL查询在移动设备上执行了两次,而我在任何桌面上都没有这个问题。因此,为了测试是否真的如此,我在每次数据库查询后都进行了console.log
。在heroku log
我看到通过我的手机进行的SQL查询被执行了两次。
正如您在下面所见,请求是关于您是否已经关注某人。此请求有很多嵌套的SQL查询。我真的不知道如何正确地构建它,因此问题可能在于此。返回一个字符串,其中说明了您与您尝试关注/取消关注的用户之间的新关系。
所以我正在尝试做什么:
检查您是否关注此人。如果是这种情况,请取消关注用户。返回。
然后,检查用户是否拥有公共帐户或私人帐户。如果他/她有公共帐户,您可以直接关注此人。回报率。
之后,检查您是否已尝试使用锁定关注用户。如果是这种情况,请删除该请求。回报率。
如果不是(3),请提出请求。回报率。
当我在计算机上发出此请求时,我查看了显示以下内容的heroku logs
:
2018-03-31T18:13:18.182665+00:00 app[web.1]: execution 1
2018-03-31T18:13:18.203767+00:00 app[web.1]: execution 3
2018-03-31T18:13:18.204511+00:00 app[web.1]: POST /user/follow 200 55.619 ms - 8
2018-03-31T18:13:18.227543+00:00 app[web.1]: execution 4
然而,当我在手机上做同样的事情时,显示出来了:
2018-03-31T18:16:24.776125+00:00 app[web.1]: execution 1
2018-03-31T18:16:24.786991+00:00 app[web.1]: execution 1
2018-03-31T18:16:24.793902+00:00 app[web.1]: execution 3
2018-03-31T18:16:24.794795+00:00 app[web.1]: POST /user/follow 200 54.334 ms - 8
2018-03-31T18:16:24.802526+00:00 app[web.1]: execution 3
2018-03-31T18:16:24.803279+00:00 app[web.1]: POST /user/follow 200 46.972 ms - 8
2018-03-31T18:16:24.816062+00:00 app[web.1]: execution 4
2018-03-31T18:16:24.821743+00:00 app[web.1]: execution 4
为了清楚执行哪个SQL语句,我为每个SQL语句创建了一个console.log
,名称不同。 ('execution1','execution2'等)。
// Check the relationship between the user and the search-user
router.post('/follow', function(req, res, next) {
// Check if the user is logged in
if (!req.session.user) {
return;
}
// Create connection with database
pool.getConnection(function (err, database) {
if (err) throw err;
// Check if you already follow the user
var sql = "SELECT * FROM Followers WHERE user = " + mysql.escape(req.session.user) + " AND following = " + mysql.escape(req.session.userSearch);
database.query(sql, function(err, result, fields) {
console.log("execution 1");
if (err) throw err;
// You follow the user if the if-statement is true
if (result.length != 0) {
// Unfollow the person
var sql = "DELETE FROM Followers WHERE user = " + mysql.escape(req.session.user) + " AND following = " + mysql.escape(req.session.userSearch);
database.query(sql, function(err, result, fields) {
console.log("execution 2");
if (err) throw err;
database.release();
});
return res.send("FOLLOW");
};
// Check if the person has a lock on its account
var sql = "SELECT private FROM Users WHERE user = " + mysql.escape(req.session.userSearch);
database.query(sql, function(err, result, fields) {
console.log("execution 3");
if (err) throw err;
// If the account is public, then the if-statement below is true
if (result[0].private == "0") {
// Follow if the account is not private
var sql = "INSERT INTO Followers (user, following) VALUES (" + mysql.escape(req.session.user) + ", " + mysql.escape(req.session.userSearch) + ")";
database.query(sql, function(err, result, fields) {
console.log("execution 4");
if (err) throw err;
database.release();
});
return res.send("UNFOLLOW");
}
// Prepare data
var sql = "SELECT * FROM PendingFollowers WHERE user = " + mysql.escape(req.session.user) + " AND following = " + mysql.escape(req.session.userSearch);
database.query(sql, function(err, result, fields) {
console.log("execution 5");
if (err) throw err;
// Check if you already try to follow the person
if (result.length == "1") {
// Remove the request
var sql = "DELETE FROM PendingFollowers WHERE user = " + mysql.escape(req.session.user) + " AND following = " + mysql.escape(req.session.userSearch);
database.query(sql, function(err, result, fields) {
console.log("execution 6");
if (err) throw err;
database.release();
});
return res.send("FOLLOW");
}
// Follow and put it in pending
var sql = "INSERT INTO PendingFollowers (user, following) VALUES (" + mysql.escape(req.session.user) + ", " + mysql.escape(req.session.userSearch) + ")";
database.query(sql, function(err, result, fields) {
console.log("execution 7");
if (err) throw err;
database.release();
return res.send("REQUESTED");
});
});
});
})
});
});
修改
HTML:
<button class="buttonFollow" ng-click="follow()"> {{ status }} </button>
AngularJS:
// Status follower
$http.get('/user/status').
then(function(res) {
$scope.status = res.data;
});
// Follow someone
$scope.follow = function() {
$http.post('/user/follow').
then(function(res) {
$scope.status = res.data;
$http.get('/user/informationFollower').
then(function(res){
$scope.following = res.data[0].following;
$scope.followers = res.data[0].followers;
});
});
};
上面的HTML / AngularJS来自您要关注的用户的页面。因此,当完成跟随请求时,将更新目标用户的关注者/跟随者的数量。当我尝试通过计算机关注某人时,该数字会增加1(这是正常的)。但是通过我的手机,它会增加2.它在我的数据库Followers
上创建了一个重复的后续行,但id
不一样。