簡體   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