我如何使用请求?查询以获取变量

How can I use request.query to get a variable

本文关键字:获取 变量 查询 何使用 请求      更新时间:2023-09-26

在客户端代码中,我调用了getContents():

$.getJSon("/getContents", function(room){
  theRoom=$("#roomName").val();//textarea's value
...
});

在getContents(),这是在服务器端代码(index.js),我如何使用请求。查询或任何其他函数来获得theRoom(变量),以便我可以根据其标题(theRoom)获得房间的内容?

getContents ();

var getContents=function(req,res){
...
var room=?
...
roomCollection.findOne({name: room}, function(err,theRoom){...
  res.send(theRoom)
});
...
}

根据JSON的结果,下面可能是一个很好的开始:

$.getJSON("/getContents", function(data){
  var theRoom=$("#roomName").val();//textarea's value
  $.each(data.rooms, function( key, room) {
    if(room.title == theRoom)
       alert(room); 
  });
});