【问题标题】:Why am i getting parent is not defined when trying to add relation with parse.com node sdk为什么我在尝试添加与 parse.com 节点 sdk 的关系时未定义父级
【发布时间】:2015-09-18 22:19:52
【问题描述】:

我已经保存了 myreferenceobject 并且在 sucess 函数中,我试图运行一个循环并创建许多其他对象,保存为与引用对象的“关系”。

                  // already have a reference to myreferenceobject

                  myobject.set("Name", artists_input["Name"]);
                  myobject.set("JbId", 12334);
                  myobject.save(null, {
                    success: function(myobject) {

                      console.log('New object created with objectId: ' + artist.id);
                      var relation = myreferenceobject.relation("Objects");
                      relation.add(myobject);
                      myreferenceobject.save();

                    },
                    error: function(myobject, error) {
                      // Execute any logic that should take place if the save fails.
                      // error is a Parse.Error with an error code and message.
                      console.log('Failed to create new myobject, with error code: ' + error.message);
                    }
                  });

当我运行脚本时,我收到以下错误

ReferenceError: parent is not defined
 at ParseRelation.add 
/home/myapp/node_modules/parse/lib/node/ParseRelation.js:102
      return parent;
             ^

我遵循了 parse 提供的语法,但我仍然收到此错误

有什么想法吗?

这是一个更简单的例子,但也失败了

  var TestObject = Parse.Object.extend("TestObject");
  var testObject = new TestObject();
  testObject.set("Name", "testName");

  testObject.save(null, {
    success: function(testObject) {
      // Execute any logic that should take place after the object is saved.
      console.log('New testObject created with objectId: ' + testObject.id);
      var TestObject2 = Parse.Object.extend("TestObject2");
      var testObject2 = new TestObject2();
      testObject2.set("Name", "testName2");
      testObject2.save(null, {
        success: function(testObject2) {
          // Execute any logic that should take place after the object is saved.
          console.log('New testObject2 created with objectId: ' + testObject2.id);
          var relation = testObject.relation("TestObjectRelation");
          console.log('Make sure the relation exists ' + testObject.relation("TestObjectRelation"));

          relation.add(testObject2);
          testObject.save();
        },
        error: function(testObject2, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and message.
          console.log('Failed to create new testObject2, with error code: ' + error.message);
        }
      });

    },
    error: function(venue, error) {
      // Execute any logic that should take place if the save fails.
      // error is a Parse.Error with an error code and message.
      console.log('Failed to create new testObject, with error code: ' + error.message);
    }
  });

【问题讨论】:

  • 如果myreferenceobject 没有名为“对象”的列,我希望看到这个
  • 不是这样的,确实有专栏,我搜过看到有人抱怨在python sdk中有类似的...专栏是def那里
  • 你能把myObject、myreferenceobject 和artist 的全部历史贴出来吗?每个是如何创建的?我对已构建但尚未保存的对象特别感兴趣。
  • @danh ...你要求它,我发布了代码,但我意识到它非常令人困惑我试图也描述逻辑

标签: node.js parse-platform relation


【解决方案1】:

失败可能与发布的代码无关。

如果testObjecttestObject2 有效,并且如果testObject 有一个名为TestObjectRelation 的列,并且该列设置为与TestObject2 类的关系,那么OP 代码应该可以工作。但如果不满足其中任何一个条件,它就会失败。

代码的一个主观问题是它的命名和结构令人困惑,因此很难通过阅读它来判断条件是否成立以及它是否做正确的事情。

除非我了解域,否则我无法帮助命名,但至少我们可以使代码结构易于理解。以这种方式编写它可以解决问题或让您发现它的起源......

function createTestObjectNamed(name) {
    var TestObject = Parse.Object.extend("TestObject");
    var testObject = new TestObject();
    testObject.set("Name", name);
    return testObject.save();
}

function createTestObject2Named(name) {
    var TestObject2 = Parse.Object.extend("TestObject2");
    var testObject2 = new TestObject2();
    testObject2.set("Name", name);
    return testObject2.save();
}

function relateTestObjects(testObject, testObject2) {
   var relation = testObject.relation("TestObjectRelation");
   relation.add(testObject2);
   return testObject.save();
}

function createAndRelateTestObjects() {
    var testObject;
    return createTestObjectNamed("MyName").then(function(result) {
        testObject = result;
        return createTestObject2Named("MyName2");
    }).then(function(testObject2) {
        return relateTestObjects(testObject, testObject2);
    });
}

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,但有效的是将解析库升级到 1.6.4

    “在 1.6.4 中解决” https://github.com/ParsePlatform/Parse-SDK-JS/issues/4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2015-05-06
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多