0

我已经使用 node.js 和 postgresql 创建了一个应用程序,以根据请求的用户 ID 显示用户城市。如果我在浏览器中点击此 URL(localhosthost:7070/user/userId/city)意味着使用快速获取请求我是调用方法以使用请求的用户 ID 从数据库中获取请求的用户城市。我得到了正确的输出,没有任何问题。但我面临着其他一些问题。

问题

首先,在浏览器中点击 localhost:7070/user/1/city URL 意味着它正在提供输出,结果也显示在我的页面中。但是如果我再次在浏览器中点击 localhost:7070/user/2/city URL 意味着我是没有收到任何错误和任何输出,但我的页面总是在加载。所以如果我想再次为其他用户执行此操作,我必须重新启动我的节点服务器。然后只有它可以工作。任何人都可以帮助我确切的问题是什么。

在这里,我附上了我的代码...

服务器.js

var pg = require('pg'); 
var conString =require('./config');
var client = new pg.Client(conString);
var queryModule=require('./query');
var express=require('express');
var app=express();
app.use(express.static(__dirname + '/public'));
app.listen(7070);

console.log('Server started at port 7070.');
app.get('/user/:id/city',function(req, response){
    client.connect(function(err, result){
        if(!err){
            var userId=req.param('id');
            queryModule.findCityByUserId(client,userId,function(err,res){
                if(!err){
                    console.log('done');
                    response.send('Requested user city is '+res);
                    return;
                }
                console.log('Error while finding the city of the user ' + err);
            });
        } else {
            console.log('error while connecting client '+err);
        }
    });
 });

查询.js

 function Query(){
 }

module.exports=Query;

Query.findCityByUserId = function(client, id, cb){
    client.query("SELECT city FROM userinfo WHERE id=" + id, function(err,res){
        if(err){
            return cb(err);
        }
        client.end();
        var row = res.rows['0'];
        console.log("row length: " + res.rows.length);
        Object.keys(row).forEach(function(key){
            cb(null, row['city']);
        });
    });
};
4

1 回答 1

-1

我刚开始使用节点。

问题可能是你没有用 res.end(); 结束你的回复。

于 2015-06-09T15:14:00.077 回答