gpt4 book ai didi

javascript - 如何将辅助函数的值返回到 server.js NodeJS

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:39 24 4
gpt4 key购买 nike

对于我的项目,我有一个 server.js,它调用辅助函数 place-search.js,如下所示。

var express = require('express');
var server = express.Router();

var placeSearch = require("./helpers/place-search");
var obj = "hello";

server.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});

server.post('/', function(req, res) {
/* get the object passed by the client's post request */
obj = req.body;

//console.log("Obj: " + obj);

/* send the confirmation back to the client */
res.status(200).send("body");
placeSearch.placeSearch(obj);
});

module.exports.server = server;

这是我的 place-search.js :

var config = require("./config.js");
var Promise = require('bluebird');
var DistanceMatrix = require("./distance-matrix.js");
var GooglePlaces = Promise.promisifyAll(require("googleplaces"));
var googlePlaces = new GooglePlaces(config.apiKey, config.outputFormat);
var extract = require('./extract.js');
var combination = require('./combination_ver2.js');
var permutation = require('./permutation.js');

function placeSearch(obj) {

console.log("Inside place search!");

/**
* Place search - https://developers.google.com/places/documentation/#PlaceSearchRequests
*/
var arr = [];
var count = 0;
var rad = obj["radius"];

console.log("radius: " + rad);
var loc = obj["location"];
console.log("Location: " + loc);
var mode = obj["mode"];

var params = obj["params"];

/* client's keywords */
var arr;
var ar = [];
for (var i = 0; i < params; i++) {
arr[i] = obj[i];
console.log(arr[i]);
var param = {
location: loc,
radius: rad,
mode: mode,
keyword: arr[i]
};
ar.push(param);
}

console.log("before promises");

var promises = ar.map(function(name) {
return googlePlaces.placeSearch(name, function(response) {
arr.push(response);
console.log(response);
console.log(count++);
//If all responses have been returned
//Find combos and pass to distance-matrix
if (count == ar.length) {
var Matrix = new Array();
var result = new Array();

//to extract only lat and lng from arr.results
//Matrix = extract.extract(arr);
result = combination.combination(arr);

// NOW RESULT IS THE ARRAY OF ALL COMBINATION

// NOW RESULT IS THE ARRAY OF COMBINATIONS OF latlng pairs AND PASS IT TO FRONTEND
/*result.forEach(function(combo, index) {
console.log("combo" + combo)
DistanceMatrix.distanceMatrix(mode, combo, result.length);
});*/


// IF YOU WANT TO SEE PERMUTATION
//permutation.permutation(result);

console.log("combination results: " + result);

}

})
});

}

module.exports.placeSearch = placeSearch;

我的问题是我不知道如何将结果变量传递回 server.js,以便我可以将该结果用作另一个辅助函数的输入。我一辈子都不知道该怎么做。任何帮助将不胜感激。

最佳答案

嗯,我没有看到你的 placeSearch 函数现在返回任何内容,也没有执行任何类型的回调。您的 placeSearch 函数应该公开一个回调参数,一旦您得到想要发回的答案,就会调用该参数。

然后,您的服务器文件将对该回调采取操作。缩写您的代码,它看起来像这样:

server.post('/', function(req, res) {
/* get the object passed by the client's post request */
obj = req.body;

//console.log("Obj: " + obj);
placeSearch.placeSearch(obj, function(error, data){
/* send the data back to the client */
res.status(200).send(data);
});
});

为了支持这一点,您的 placeSearch 函数必须在适当的时候调用其回调:

function placeSearch(obj, callback){

/* all the stuff you do to assemble your data */
// if (there_is_some_error):
if(err) return cb(err);

// when you have all your data available, no error has occurred
return cb(null, data);
}

您可能会注意到的其他事情是您的 ar.map 将无法按您预期的方式工作。 ar.map 是一个同步函数,您在内部调用异步代码...不会按照您想象的方式工作。这篇文章有点长,但您应该查看 npm 中的 async 库来管理一组异步请求来收集一个组合结果。

关于javascript - 如何将辅助函数的值返回到 server.js NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com