I have a function called makeNewNode ()
, which should take a pars geopoint object and a custom) Pod object node in which there is a GeoPoint object with the key "location" and the pointer for the second custom pars object with the key "stop". What this function should do is essentially "snap" for a node if any of the SNAP_RADIUS
is its GeoPoint if not, then a tap "stop" pointer and parameter geoPoint
. Return a new node with a "place" of
I have some problems with this code.
The first is that it seems that she always fails to return anything. Parse query always gives success, which is good. However, it only gives some useful when it is "snaps". Otherwise it returns only an undefined result
variable.
The second (and the main) is that without rectification or not, the function never returns anything similar to the node object, 'I hope it is me wide of console.log
Through the use goes to believe that the node is never changed by the verbs within the success function of the query.
In relation to the third, in the previous general sense, every time I tried to return an object from a function in JavaScript, I did not work according to the way I expected it. Every time I try to change a variable from within the success function of a query, there really is no change, I'm slightly new to javascript in this deeper because my previous experience is a little more light.
This is a troublesome code.
function makeNewNode (geoPoint) {var node = {}; Var node = Parse.Object.extend ("nodes"); Var query = new parsing. Configuration (node); Query.withinMiles ("location", geoPoint, SNAP_RADIUS); Query.first ({Success: Work) {console.log (results); console.log (results + ""); if (result == undefined) {node = new node (); node.set ("location ", GeoPoint); node.set (" stop ", null); node.save (); console.log (node.id);} else {node = results;}}, error: function (error) {console log ("Failed to create a node. Error:" + Error. Message + ".");}}); Return node; }
And here's where I call it.
var geoPoint = new parse. Geopoint (location.lat (), location.lng ()); Var newnode = makeNewNode (geoPoint);
Any thoughts or suggestions about my code or none of the three questions are highly appreciated.
Using a clean way to make promises, so that without the newly created object can be saved Additional callback can be of parameter and without the intense and intense nests of success tasks. Apart from this, the collar might want to make some constellation as part of some large part of the ascope.
A promise version looks like this:
// Promises that, upon completion, creates a new node with // on the geo-point, Or, if a node exists in SNAP_RADIUS, then it returns the node function mannote node (geopoint) {var node = pars.office. Extensions ("nodes"); Var query = new parsing. Configuration (node); Query.withinMiles ("location", geoPoint, SNAP_RADIUS); Return Query.First () Then (function (node) {if (node) {node = new node (); node .set ("location", geographic); node .set ("stop", empty);} return (node.is new ))? Node.save (): Pars promo.se (node);}); }
The caller can now add it to other promises.
Edit - Here's how you say it, we say that we get Bhupoint in the second asynchronous call, then ...
< Pre> var geoPoint = new parse. Geopoint (location.lat), location.lng ()); MakeNewNode (geoPoint). Then (function (new node) {console.log ("new node id" + newNode.id);}, function (error) {console.log ("error" + error message);});
What is happening with all returns? A promise is a placeholder for the result, it returns the returning collar and the object in which they can attach to the completion function (using the then () method) This complete callback can return a promise (this is also an internal return ), So that they can be chained arbitrarily. Again there is an optional callback to handle the second parameter () failure.
No comments:
Post a Comment