繁体   English   中英

Javascript似乎没有正确返回Parse对象

[英]Javascript doesn't seem to return Parse objects properly

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

我对这段代码有几个问题。

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

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

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

这是麻烦的代码。

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);

任何有关我的代码或三个问题之一的想法或建议都非常感谢。

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

承诺版本如下所示:

// 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);
    });
}

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

编辑 - 这就是你如何称呼它。 让我们说我们在另一个异步调用中得到geoPoint,然后......

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()的第二个参数是一个可选的回调函数来处理失败。

我不知道您是否了解异步问题,但它们可能是问题所在。

function makeNewNode(geoPoint) {
  var Node = Parse.Object.extend("Nodes");
  var node = new Node();
  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.set("location",geoPoint);
        node.set("stop",null);
        node.save(function() {
          console.log(node.id);
        });
      } else {
        node.set("location", results.get("location"));
        node.set("stop", results.get("stop"));
      }
    },
    error: function(error) {
      console.log("Failed to create a node. Error: " + error.message + ".");
    }
  });
  return node;
}

query.first()是一个异步函数。 当您的函数返回时,异步函数可能没有调用您的回调。 在这种情况下,返回值将是您在开头定义的对象{}

当你将一个新的值(后来new Node()等),以node ,返回值仍然是以前的对象,而不是分配给新的对象node

简单的解决方法是使用预先指定的对象进行返回,并在以后更新其内容,就像上面的示例一样。

但是,对于异步代码段,这绝不是一个好的解决方案。 您应该使用回调或承诺来返回新Node

使用回调就像:

function makeNewNode(geoPoint, callback) {
  var Node = Parse.Object.extend("Nodes");
  var node;
  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.set("location", geoPoint);
        node.set("stop", null);
        node.save(function() {
          console.log(node.id);
          callback(null, node);
        });
      } else {
        callback(null, results);
      }
    },
    error: function(error) {
      console.log("Failed to create a node. Error: " + error.message + ".");
      callback(error);
    }
  });
}

你可以像以下一样使用它:

makeNewNode(geoPoint, function(err, newNode) {
  // use your new Node here.
})

暂无
暂无

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

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