从 res 检索 JSON 时遇到问题

Having trouble retrieving JSON from res

我正在使用 node、express 和 mongoose。

我有一个函数可以在数据库中执行搜索并以 JSON 格式发送响应:

res.jsonp(search_result);

这会在浏览器中将正确返回的结果显示为 JSON 对象。我的问题是,如何获得 JSON 对象?

我试过返回 search_result 但这给了我 null(可能是因为异步)。我也不想编辑当前函数(它是由其他人编写的)。我正在调用该函数并得到一个充满 JSON 的屏幕,这是 res.jsonp 应该做的。

提前致谢。

只需创建一个将此 JSON 作为参数的新函数并将其放入旧函数中。示例:

// Old function
app.get('/', function(req,res){

  // recieve json
  json_object = .....

  // Get json to your function
  processJson(json_object);

  // Old function stuff
  .
  .
  .

});

function processJson(json_object) {

  // Here you'll have the existing object and can process it
  json_object...

}