gpt4 book ai didi

javascript - 如何在我的 Node.js Web 服务中包含另一个参数?

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

抱歉,如果这是一个乏味的问题,我才刚刚开始了解 JS/node.js 如何处理 post/get 消息。

我正在编写一个 Web 服务,根据用户的纬度/经度和距离从 mongodb 数据库查询数据。我在前端代码中编写了以下函数:

// Take query parameters and incorporate into a JSON queryBody
$scope.queryUsersFromLast24Hours = function(){

// Assemble Query Body
queryBody = {
longitude: parseFloat($scope.formData.longitude),
latitude: parseFloat($scope.formData.latitude),
distance: parseFloat($scope.formData.distance)
};


// Post the queryBody to the /query POST route to retrieve the filtered results
$http.post('/queryUsersFromLast24Hours', queryBody)

// Store the filtered results in queryResults
.success(function(queryResults){

console.log(JSON.stringify(queryBody));

// Pass the filtered results to the Google Map Service and refresh the map
gservice.refresh(queryBody.latitude, queryBody.longitude, queryResults);

// Count the number of records retrieved for the panel-footer
$scope.queryCount = queryResults.length;
})
.error(function(queryResults){
console.log('Error ' + queryResults);
})
};

它从我的支持代码中调用方法/queryUsersFromLast24Hours,如下所示:

// Retrieves JSON records for all users who meet a certain set of query conditions
app.post('/queryUsersFromLast24Hours/', function(req, res){

// Grab all of the query parameters from the body.
var lat = req.body.latitude;
var long = req.body.longitude;
var distance = req.body.distance;
var dateNow = new Date(); //right now



// Opens a generic Mongoose Query. Depending on the post body we will...
var query = User.find({});

// ...include filter by Max Distance (converting miles to meters)
if(distance){

// Using MongoDB's geospatial querying features. (Note how coordinates are set [long, lat]
query = query.where('location').near({ center: {type: 'Point', coordinates: [long, lat]},

// Converting meters to miles. Specifying spherical geometry (for globe)
maxDistance: distance * 1609.34, spherical: true});
}
console.log(dateNow);
dateNow.setHours(dateNow.getHours()-24); //24 hours from now
console.log(dateNow);
query = query.where('created_at').gte(dateNow); //gte - greater then equal


// Execute Query and Return the Query Results
query.exec(function(err, users){
if(err)
res.send(err);

// If no errors, respond with a JSON of all users that meet the criteria
res.json(users);
});
});

但是现在,如果我想让它更加通用,返回的数据不是过去24小时的数据,而是用户给出的最后xx小时的数据,我应该如何修改?

我想创建一个 Web 服务,它不仅可以获取纬度/经度/距离数据,还可以获取小时数作为 POST 消息,并返回包含正确数据的 json。那么我应该如何修改我的前端/后端代码以包含此更改?

另外,现在我从 GUI 中调用它,如下所示:

<button type="button" class="btn btn-danger" ng-click="queryUsersFromLast24Hours()">Show data from last hour</button>

那么我如何修改它以传递将包含在后端查询搜索中的特定小时数?

非常感谢您的帮助!

最佳答案

快速回答:

前端:

// Take query parameters and incorporate into a JSON queryBody
$scope.queryUsers = function(){

// Assemble Query Body
queryBody = {
longitude: parseFloat($scope.formData.longitude),
latitude: parseFloat($scope.formData.latitude),
distance: parseFloat($scope.formData.distance)
hours : Number($scope.formData.hours)
};


// Post the queryBody to the /query POST route to retrieve the filtered results
$http.post('/queryUsers', queryBody)

// Store the filtered results in queryResults
.success(function(queryResults){

console.log(JSON.stringify(queryBody));

// Pass the filtered results to the Google Map Service and refresh the map
gservice.refresh(queryBody.latitude, queryBody.longitude, queryResults);

// Count the number of records retrieved for the panel-footer
$scope.queryCount = queryResults.length;
})
.error(function(queryResults){
console.log('Error ' + queryResults);
})
};

后端:

// Retrieves JSON records for all users who meet a certain set of query conditions
app.post('/queryUsers/', function(req, res){

// Grab all of the query parameters from the body.
var lat = req.body.latitude;
var long = req.body.longitude;
var distance = req.body.distance;
var hours = req.body.hours
var dateNow = new Date(); //right now



// Opens a generic Mongoose Query. Depending on the post body we will...
var query = User.find({});

// ...include filter by Max Distance (converting miles to meters)
if(distance){

// Using MongoDB's geospatial querying features. (Note how coordinates are set [long, lat]
query = query.where('location').near({ center: {type: 'Point', coordinates: [long, lat]},

// Converting meters to miles. Specifying spherical geometry (for globe)
maxDistance: distance * 1609.34, spherical: true});
}
console.log(dateNow);
dateNow.setHours(dateNow.getHours()- hours); // XX hours from now
console.log(dateNow);
query = query.where('created_at').gte(dateNow); //gte - greater then equal


// Execute Query and Return the Query Results
query.exec(function(err, users){
if(err)
res.send(err);

// If no errors, respond with a JSON of all users that meet the criteria
res.json(users);
});
});

很抱歉,如果我误解了您的问题,但我只是想给您一些提示,此查询看起来像 GET 而不是 POST,您可以使用查询参数或路径参数。

关于javascript - 如何在我的 Node.js Web 服务中包含另一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888849/

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