简体   繁体   English

解析云代码geoPoint查询Javascript语法

[英]Parse Cloud Code geoPoint Query Javascript Syntax

I am attempting to write a Parse Cloud Code function where the parameter is a list of Objects that each include a geoPoint member. 我正在尝试编写Parse Cloud Code函数,其中参数是每个包含geoPoint成员的对象列表。 For each item in the list, I will search the Parse data store for an existing item in a 1 mile radius with the same name. 对于列表中的每个项目,我将在Parse数据存储中搜索1英里半径内具有相同名称的现有项目。 If the item does not exist, then create the item and save it to the data store. 如果该项目不存在,则创建该项目并将其保存到数据存储。

My Function 我的功能

/**
 *  Take a list of Place and check for existence in Parse.
 *  If it doesn't already exist, add it.
 *  @see https://www.parse.com/questions/access-distance-on-a-geoquery
 *  @param {JSON Place.PlaceSearch}
 *  @return none
 */
 function addConvertedApiResult(placeData) {
    for ( var i = 0; i < placeData.length; i++ ) {
        // look near loc for existing name
        var loc = new Parse.GeoPoint(placeData[i].lat, placeData[i].lon)

        var query = new Parse.Query("Place");
        query.withinMiles("location", loc, 1);
        query.equalTo("name", placeData[i].name);

        console.log("placeData[i].name:"+placeData[i].name);
        query.find({
            success: function(results) {
                // results contains a list of places that are within 1 mile
                // and have the same name
                console.log("results.length = "+results.length);
                if(results.length < 1) {
                    // add the place
                    var Place = Parse.Object.extend("Place");
                    var place = new Place();

                    place.set("en", placeData[i].name);
                    place.set("location", loc);
                    place.set("moreInfo", placeData[i].moreInfo);

                    place.save();
                }
            },
            error: function(error) {
                // There was an error.
                console.log("error = "+error.message);
            }
        });

        console.log("leaving addConvertedApiResult");
    }
}

My request to a Cloud Function that calls addConvertedApiResult 我对调用addConvertedApiResult的Cloud Function的请求

(Must escape '"' from windows command line) (必须从Windows命令行中转义'“')

curl -X POST 
-H "X-Parse-Application-Id: <key>" 
-H "X-Parse-REST-API-Key: <key>" -H "Content-Type: application/json" 
-d "{\"name\":\"Storm+Mountain\",\"lat\":44.0760,\"lon\":-103.2280,\"limit\":5,\"radius\":25}" https://api.parse.com/1/functions/getPlace
{"result":[{"lat":43.95483,"lon":-103.36869,"moreInfo":"<apiUrl>/item.php?c=1\u0026i=3439","name":"Storm Mountain"}]}

Resulting Parse Info Log 产生的解析信息日志

I2015-03-03T05:56:26.905Z] v99: Ran cloud function getPlace with:
  Input: {"lat":44.0760,"limit":5,"lon":-103.2280,"name":"Storm+Mountain","radius":25}
  Result: [{"name":"Storm Mountain","lat":43.95483,"lon":-103.36869,"moreInfo":"<moreInfoUrl>"}]

I2015-03-03T05:56:27.434Z] placeData[i].name:Storm Mountain

I2015-03-03T05:56:27.435Z] leaving addConvertedApiResult

There are 4 existing points in the data store that should be returned but none of them share the same name. 数据存储中有4个现有点应该返回但没有一个共享相同的名称。 The function does not seem to execute the query.find method. 该函数似乎不执行query.find方法。 I do not see the log messages from the success: or error: functions. 我没有看到成功的日志消息:或错误:函数。 If I understand correctly, these functions should allow me to execute code on the query results. 如果我理解正确,这些函数应该允许我在查询结果上执行代码。

How could I confirm there are no results from the query if console.log does not seem to work? 如果console.log看起来不起作用,我如何确认查询没有结果?

I've been up and down the web trying different variations on this syntax. 我一直在网上尝试不同的语法变体。 Is my syntax correct? 我的语法是否正确?

Thanks. 谢谢。

Update 更新

I've been working on this problem again and was excited to learn about Promises . 我一直在努力解决这个问题,很高兴能够了解Promises Unfortunately, my situation has not changed. 不幸的是,我的情况没有改变。 I am now calling these functions from my addConvertedApiResult function. 我现在从addConvertedApiResult函数调用这些函数。 I've tested/debugged them with a driver written in a local javascript file using Chrome Developer tools, everything runs nicely. 我使用Chrome Developer工具使用本地javascript文件编写的驱动程序测试/调试它们,一切运行良好。 But, when I deploy this code to Parse Cloud Code execution seems to disappear into the ether after I call .find(). 但是,当我将此代码部署到解析云代码时,我调用.find()后,执行似乎消失在以太网中。

I understand that code execution is asynchronous on CC but I'm under the impression that the use of the .then() function should overcome that limitation. 我知道代码执行在CC上是异步的,但我的印象是使用.then()函数应该克服这个限制。

I hope someone can explain to me what I'm missing. 我希望有人可以向我解释我缺少的东西。

Thanks. 谢谢。

/**
 *  Take a Place and check for existence in Parse.
 *  If it doesn't already exist, add it.
 *  @see https://www.parse.com/questions/access-distance-on-a-geoquery
 *  @see https://parse.com/docs/js_guide#promises-chaining
 *  @param {JSON Place.PlaceSearch}
 *  @return none
 */
function addNewPlace(place) {
    // look near loc for already existing place with same name
    var loc = new Parse.GeoPoint(place.lat, place.lon)
    var query = new Parse.Query('Place');
    query.withinMiles('location', loc, 1);
    query.equalTo('en', place.name);
    query.find().then( function(results) {
        if(results.length < 1) {
            console.log(place.name+" does not exist")
            var Place = Parse.Object.extend("Place");
            var newPlace = new Place();

            var loc = new Parse.GeoPoint(place.lat, place.lon)
            newPlace.set('en', place.name);
            newPlace.set('location', loc);
            newPlace.set('moreinfo', place.moreinfo);

            return newPlace.save();
        }
    });
}

/**
 *  Take a list of Place and check for existence in Parse.
 *  If it doesn't already exist, add it.
 *  @see https://www.parse.com/questions/access-distance-on-a-geoquery
 *  @see https://parse.com/docs/js_guide#promises-chaining
 *  @param {JSON Place.PlaceSearch}
 *  @return none
 */
function addConvertedApiResult(placeData) {
    var _ = require('underscore');

    console.log("placeData.legth:"+placeData.length);
    _.each(placeData, function(place) {
        addNewPlace(place);
    });
}

The code snippets I provided above are syntactically correct. 我上面提供的代码片段在语法上是正确的。 My problem though, was that it took me awhile to get my head wrapped around the concept of programming sequential asynchronous calls to Parse and other APIs from CloudCode. 我的问题是,我花了一些时间来解决编程对来自CloudCode的Parse和其他API的顺序异步调用的概念。 Ultimately I was able to accomplish what I wanted using one single function with a string of promises using the following pattern. 最终,我能够使用以下模式使用带有一串承诺的单个函数来完成我想要的任务。

someCall( function(someObject) {
    // do some stuff
    return result;

}).then( function(resultOfPrev) {
    // do some more stuff
    return result;

}).then( function(resultOfPrev) {
    // create some nested promises

    // Create a trivial resolved promise as a base case.
    var queryPromise = Parse.Promise.as();

    _.each(resultsOfPrev, function(thing) {
        // For each item, extend the promise with a function to ...
        queryPromise = queryPromise.then( function() {

            var query = new Parse.Query(Thing);
            query.equalTo("name", thing.name);

            return query.find().then( function(result) {

                var savePromise = Parse.Promise.as();
                savePromise = savePromise.then( function() {

                    var saved = false;
                    if(result.length < 1) {
                        // then save the thing
                        var newThing =  new Thing();
                        newThing.set('name', thing.name);
                        return newThing.save();
                    } else {
                        return false;
                    }
                });

                return savePromise;
            });
        });
    });

    return queryPromise;

}).then( function(resultsOfPrev) {
    // response must not be called until
    // the end of the chain.
    response.success( resultsOfPrev);

}.then( function() {
    // i realy don't understand why but,
    // I need this empty function at the 
    // tail of the chain.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM