基本上我遇到了节点应用程序的排序问题。我需要创建一个聊天室。 三个组件:1。)server.js 2.)RoomHandler.js 3.)DBService.js
当用户点击createRoom时,它应该返回在数据库中生成的roomId。 [此操作由2个mysql部分组成:
query1 = insert into groupchatroom(room_name,room_description) values('test1','Test room dummy room');
query2 = select groupchatroomid from groupchatroom where room_name='test1';
只有1次成功,2才会被解雇。
这里是代码序列的方式
当请求到达server.js时:
app.post('/createGroupRoom',function(req,res){
var roomData = JSON.parse(req.query.room);
var roomId=roomHandler.createGroupRoom(roomData.roomName,roomData.roomDesc);
res.json({roomNum: roomId});
});
Inside RoomHandler:
var that = this;
var p = new Promise(function(resolve,reject){
that.dbService.executeQuery(query1,resolve,reject);
});
p.then(function()
{
that.dbService.executeQuery(query2,p.resolve,p.reject);
}).then(function(resultSet){
//resultSet is always coming as undefined
if(resultSet[0] && resultSet[0].id_groupchatroom)
{
console.log("Created groupRoom "+resultSet[0].id_groupchatroom);
return resultSet[0].id_groupchatroom;
}
}).catch(function(err){}).catch(function(err){; });
DBService内部:
DBService.prototype.executeQuery = function(query,callback,callbackerr){
console.log("Query to DB : "+query);
var that = this;
this.pool.getConnectionAsync().then(function(connection){
that.Promise.promisifyAll(connection);
connection.queryAsync(query).then(function(resultSet){
connection.release();
callback(resultSet);
}).catch(function(err){
connection.release();
callbackerr(err);
});
}).catch(function(err){
connection.release();
callbackerr(err);
});
};
我无法在第二个Promise链中获取第二个查询的resultSet。而且它总是给出以下错误,回调不是同一个地方的函数。可能是p.resolve不是一个函数。但我对如何构造上面的代码毫无头绪。如果它是Java应用程序,那将简单如下。
server.java:
roomHandler.createRoom(roomData,httpReq,httpResp);
RoomHandler.java:
public void createRoom(Room roomData, HttpRequest req, HttpResponse res)
if(dbService.executeQuery(query1))
{
rs = dbService.executeQuery(query2);
if(rs!=null)
res.end(rs.next());
}
我不确定我是否在尝试过这里的代码!但最好从开始对代码进行排序。任何帮助/教程将不胜感激。
答案 0 :(得分:1)
你的代码除了承诺链中缺少的回报之外还有几个问题,对于初学者来说,用承诺停止包装承诺:
DBService.prototype.executeQuery = function(query){
console.log("Query to DB : "+query);
var that = this, connection;
return this.pool.getConnectionAsync().then(function(_connection){
connection = that.Promise.promisifyAll(_connection);
return connection.queryAsync(query);
}).then(function(resultSet){
connection.release();
return resultSet;
}).catch(function(err){
if(connection) connection.release();
throw err;
});
}
和RoomHandler代码类似:
var that = this;
return this.dbService.executeQuery(query1).then(function(){
return that.dbService.executeQuery(query2);
}).then(function(resultSet){
//resultSet is always coming as undefined
if(resultSet[0] && resultSet[0].id_groupchatroom)
{
console.log("Created groupRoom "+resultSet[0].id_groupchatroom);
return resultSet[0].id_groupchatroom;
}
}).catch(function(err){});
修改强>
你的server.js会是这样的:
app.post('/createGroupRoom',function(req,res){
var roomData = JSON.parse(req.query.room);
roomHandler.createGroupRoom(roomData.roomName,roomData.roomDesc)
.then(function(roomId){
res.json({roomNum: roomId})
});
});