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

Javascript doesn't seem to return Parse objects properly

本文关键字:返回 对象 Parse 没有 Javascript      更新时间:2023-09-26

我有一个名为makeNewNode()的函数,该函数应该接受一个Parse GeoPoint对象,并返回一个称为node的(自定义)Parse对象,该对象包含一个带有关键字"location"的GeoPoint对象和一个指向另一个带有键"stop"的自定义Parse对象的指针。如果GeoPoint的SNAP_RADIUS中有一个节点,则该函数应该"捕捉"到该节点。如果不是,则返回一个全新的节点,其中包含一个空的"停止"指针和参数geoPoint的"位置"。

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

首先,它似乎总是什么都不回。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);

任何关于我的代码或这三个问题的想法或建议都将不胜感激。

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

一个promise版本如下:

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

呼叫者现在可以将此与其他承诺相关联。

EDIT-下面是它的调用方式。假设我们在另一个异步调用中获得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()是一个异步函数。当您的函数返回时,异步函数可能没有调用您的回调。在这种情况下,返回值将是您在开头定义的对象{}

稍后,当您为node指定一个新值(new Node()等)时,返回的值仍然是以前的对象,而不是指定给node的新对象。

简单的修复方法是使用预先分配的对象进行返回,然后更新其内容,就像我上面的例子一样。

然而,对于异步代码片段来说,这从来都不是一个好的解决方案。您应该使用回调或promise来返回新的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.
})