Azure移动服务中的自定义API。 response.send有时返回结果,有时不返回。 (内部记录的代码)

时间:2014-09-05 03:39:10

标签: azure azure-mobile-services

我编写了一个自定义API,当我从iOS应用程序调用时,它通过Azure移动服务搜索SQL表。该表格填充了带有纬度和经度坐标的邮政编码。给定搜索接近度,算法将在指定的邮政编码附近找到所有邮政编码。然后,它将使用这些发现的邮政编码搜索实体的信息表,并返回包含已发现邮政编码的任何实体。

我遇到的问题是我的查询有时会返回数据,有时则不然。我的邮政编码表有大约35,000个邮政编码。我能够在没有问题的情况下对邮政编码表执行操作(插入,搜索,读取....)。发现的邮政编码用于搜索的信息表仅包含一个项目,用于在发现邮政编码时测试检索。查询邮政编码表时,它始终返回数据。当使用检索到的邮政编码数据来查询信息表时,可以返回或不返回其中的测试实体。邮政编码表查询总是有效,信息表查询有时候根本没有。我知道node.js是异步的,但是我已经使用了好几天并且多次修改了我的代码。代码总是到达return.send,所以总是在邮政编码表中找到所需的邮政编码。

这是我的代码。我已将其记录下来,以便您可以更轻松地进行检查。

exports.post = function(request, response) {

var postal = request.body.Postal;
var proximity = request.body.Proximity;

var countryGeoTable;   
var geoQuery; 
if(request.body.Country == "United States")
{
    //first the table of zip codes is retrieved.
    countryGeoTable = request.service.tables.getTable('UnitedStatesGeoLocations');
    //a query is prepared that finds the zip codes within the 
    //specified proximity of a zip code, then queries the information 
    //table with thise tip codes to find entities that have those zip codes.
    geoQuery = 'SELECT * FROM UnitedStatesEntities WHERE postal IN (SELECT postal FROM UnitedStatesGeoLocations WHERE latitude > ? AND latitude < ? AND longitude > ? AND longitude < ?)';
}

//the zip code entered is retrieved from the zip code table
countryGeoTable.where({ postal : postal}).read(
{
    success: function(results) 
    {
        if (results.length > 0)
        {
           //if the zip code is in the table, then using its longitude and latitude coordinates a
           //bounding box will be created, which will be used to find all zip codes within that
           //region. The proximity passed into the script determines the size of this bounding box.
            var postalLatitude = results[0].latitude;
            var postalLongitude = results[0].longitude;
            var lat1 = postalLatitude-(proximity/69);
            var lat2 = postalLatitude+(proximity/69);
            var lon1 = postalLongitude-proximity/Math.abs(Math.cos(proximity * Math.PI/180)*69);
            var lon2 = postalLongitude+proximity/Math.abs(Math.cos(proximity * Math.PI/180)*69);                

            //the nested SELECT query will first find the zip codes, then use them to find all
            //entities within the information table that has that zip code.
            request.service.mssql.query(geoQuery,[lat1,lat2,lon1,lon2],
            {
                success: function(entities) 
                {
                    //the problem here is that the entity may or may not be retrieved.
                    if(entities.length > 0)
                    {
                        //if retrieved, then the entity will be send to the client.
                        response.send(statusCodes.OK, entities);
                    }
                    else
                    {
                        response.send(statusCodes.NOT_FOUND,{ error : "no entities found in proximity" });
                    }
                }
            });
        }
        else
        {
            request.respond(statusCodes.NOT_FOUND, 'Zip Code Not Found!');
        }
    }
}); 

};

在查找this帖子时,我已将我的代码修改为以下内容,但同样的行为仍然存在。

exports.post = function(request, response) {

var postal = request.body.Postal;
var proximity = request.body.Proximity;

var countryGeoTable;   
var geoQuery; 
if(request.body.Country == "United States")
{
    countryGeoTable = request.service.tables.getTable('UnitedStatesGeoLocations');
    geoQuery = 'SELECT * FROM UnitedStatesEntities WHERE postal IN (SELECT postal FROM UnitedStatesGeoLocations WHERE latitude > ? AND latitude < ? AND longitude > ? AND longitude < ?)';
}

entityFinder(function(entities)
{
    if (entities.length > 0) 
    {
        response.send(statusCodes.OK, entities);
    }
    else
    {
        response.send(statusCodes.OK, { message : 'no entities within this proximity' });
    }
});

function entityFinder(callback)
{
    getCoordinates(countryGeoTable,function(parameter) 
    {            
        request.service.mssql.query(geoQuery,parameter,
        {
            success: function(entities) 
            {
                callback(entities);
            }
        });
    });
}

function getCoordinates(countryGeoTable,callback)
{
    countryGeoTable.where({ postal : postal}).read(
    {
        success: function(results) 
        {
            if (results.length > 0)
            {                
                var postalLatitude = results[0].latitude;
                var postalLongitude = results[0].longitude;
                var lat1 = postalLatitude-(proximity/69);
                var lat2 = postalLatitude+(proximity/69);
                var lon1 = postalLongitude-proximity/Math.abs(Math.cos(proximity * Math.PI/180)*69);
                var lon2 = postalLongitude+proximity/Math.abs(Math.cos(proximity * Math.PI/180)*69);

                var parameter = [lat1,lat2,lon1,lon2];

                callback(parameter);              
            }
            else
            {
                request.respond(statusCodes.NOT_FOUND, 'Zip Code Not Found!');
            }
        }
    }); 
}

};

0 个答案:

没有答案