Javascript似乎没有正确返回Parse对象 [英] Javascript doesn't seem to return Parse objects properly

查看:86
本文介绍了Javascript似乎没有正确返回Parse对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 makeNewNode()的函数,该函数应该使用Parse GeoPoint对象并返回一个名为node的(自定义)Parse对象,该对象包含一个带有键的GeoPoint对象 location和指向另一个自定义Parse对象的指针,其中键为stop。如果该节点的GeoPoint的 SNAP_RADIUS 中有一个节点,那么该函数应该基本上捕捉到该节点。如果没有,返回一个全新的节点,其中包含一个空的停止指针和参数 geoPoint 的位置。

I have a function called makeNewNode() that should take a Parse GeoPoint object and return a (custom) Parse object called node that contains a GeoPoint object with the key "location" and a pointer to another custom Parse object with the key "stop." What this function should do is essentially "snap" to a node if there is one within the SNAP_RADIUS of its GeoPoint. If not, return a brand new node with a null "stop" pointer and a "location" of the parameter geoPoint.

我对此代码有几个问题。

I have a couple issues with this code.

首先,似乎总是无法返回任何东西。 Parse查询总是返回成功,这很好。但是,它只会在快照时返回可用的内容。否则它只返回一个未定义的结果变量。

The first is that it seems to always fail to return anything. The Parse query always returns success, which is good. However, it only returns something usable when it "snaps." Otherwise it just returns an undefined results variable.

第二个(和主要)是无论是否捕捉,函数永远不会返回任何远程类似于我期望的节点对象。这使我相信通过广泛使用 console.log 该查询成功函数中的操作永远不会更改或影响该节点。

The second (and main) is that regardless of snapping or not, the function never returns anything remotely similar to the node object that I'm expecting. This leads me to believe through extensive use of console.log that node is never changed or affected by the actions within the success function of the query.

第三个与更广泛意义上的前一个相关。每当我试图从Javascript函数返回一个对象时,它就没有按照我的预期行事。每次我尝试从查询的成功函数中更改变量时,实际上没有任何变化。我对这个更深入的Javascript有点新,因为我以前的经验稍微有点亮。

The third is related to the previous in a more general sense. Every time I've tried to return an object from a function in Javascript it hasn't acted the way I've expected. Every time I've tried to change a variable from within a query's success function nothing actually changes. I am a little new to this more in-depth Javascript as my previous experience has been a little more light.

这是一个麻烦的代码。

function makeNewNode(geoPoint) {
  var node = {};
  var Node = Parse.Object.extend("Nodes");
  var query = new Parse.Query(Node);
  query.withinMiles("location",geoPoint,SNAP_RADIUS);
  query.first({
    success: function(results) {
      console.log(results);
      console.log(results + "");
      if (results == 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;
}

这是我称之为的地方。

var geoPoint = new Parse.GeoPoint(location.lat(),location.lng());
var newnode = makeNewNode(geoPoint);

我们非常感谢您对我的代码或三个问题中的任何一个提出任何想法或建议。

Any ideas or suggestions about my code or either of the three questions is greatly appreciated.

推荐答案

更简洁的方法是使用promises,这样就可以在没有额外回调参数的情况下处理新创建的对象的保存而不会创建更深层次的更深层次的成功功能嵌套。此外,调用者可能希望将节点作为一组更大的异步步骤的一部分。

A cleaner way is to use promises, so that the save of the newly created object can be handled without the extra callback parameter and without creating deeper and deeper nesting of success functions. Moreover, the caller may want to make a node as part of some larger set of asynch steps.

承诺版本如下所示:

// return a promise that, when fulfilled, creates a new node with
// at the geoPoint, or, if a node exists within SNAP_RADIUS, returns that node
function makeNewNode(geoPoint) {
    var Node = Parse.Object.extend("Nodes");
    var query = new Parse.Query(Node);
    query.withinMiles("location",geoPoint,SNAP_RADIUS);
    return query.first().then(function(node) {
        if (!node) {
            node = new Node();
            node.set("location", geoPoint);
            node.set("stop", null);
        }
        return (node.isNew())? node.save() : Parse.Promise.as(node);
    });
}

调用者现在可以将其与其他承诺链接起来。

The caller can now chain this with other promises.

编辑 - 以下是您的称呼方式。让我们说我们在另一个异步调用中得到geoPoint,然后......

EDIT - Here's how you call it. Lets say we get the geoPoint in another asynchronous call, then...

var geoPoint = new Parse.GeoPoint(location.lat(),location.lng());
makeNewNode(geoPoint).then(function(newNode) {
    console.log("new node id is " + newNode.id);
}, function(error) {
    console.log("error " + error.message);
});

所有回报怎么办?承诺是结果的占位符。返回它给调用者和对象提供了一个完成函数(使用then()方法)。完成回调也可以返回一个promise(这是内部返回),这样它们就可以被任意链接。 then()的第二个参数是一个可选的回调来处理失败。

What's up with all the returns? A promise is a placeholder for the result. Returning it gives the caller and object to which they can attach a completion function (using the then() method). That completion callback can also return a promise (that's the inner return), so that they can be chained arbitrarily. The second parameter to then() is an optional callback to handle failure.

这篇关于Javascript似乎没有正确返回Parse对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆