javascript - 如何在Parse上保存具有不同ACL的Parse对象的指针?

标签 javascript pointers parse-platform relationship acl

我这里有一个非常棘手的问题;我现在至少查看了 Parse.com js SDK 三遍,但无法弄清楚这一点。

我有一个包含文本、用户、评论的问题类。 我有一个带有文本的评论类,用户。

注释放置在指向问题类的指针数组中。

Question = Parse.Object.extend('Question');
question = new Question();
question.set('text', 'my first question');

// the user posting the question
var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
question.setACL(acl);
question.set('user', Parse.User.current());
question.save();

这里一切都很好。现在,另一个用户登录应用程序并发表评论

question; // let's asume the question is fetched
Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user has public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());
comment.save();

// comment is saved we can now add it to the question's array
question.add(comment);
question.save(); // this is where everything fails... 

它说用户无权在问题对象上书写,这是正常的,但是如果我不使用新数组保存问题,如何保存评论?

最佳答案

选项 1:加入表

question; // let's asume the question is fetched

//GET OBJECT ID HERE WHEN FETCHING QUESTION
questionID = question.id;


Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user is have public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());

//NEW CODE HERE TOO
comment.save().then(function(results){
  commentID = results.id;
});;

// NOW ADD COMMENT AND QUESTION TO JOIN TABLE BY OBJECTID
JoinTable = Parse.Object.extend('JoinTable');
entry = new JoinTable();
entry.set('questionID', questionID);
entry.set('commentID', commentID);
entry.save().then(function(results){
//MIGHT WANT TO PUSH THE NEW COMMENT ONTO THE COMMENTS ARRAY 
//WHICH YOU ARE DISPLAYING WITH YOUR QUESTION
});

现在,当您加载问题时,只需通过 QuestionID 对连接表进行查询,这将为您提供与该问题关联的所有 commentID。然后使用该 commentID 查询评论表,加载并显示这些结果。

选项 2:保存带有评论的问题 ID

question; // let's asume the question is fetched

//GET OBJECT ID HERE WHEN FETCHING QUESTION
questionID = question.id;


Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user is have public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());
//SET QUESTION ID ON COMMENT
comment.set('parent', questionID);


//NEW CODE HERE TOO
comment.save().then(function(results){
  commentID = results.id;
});;

现在,当您查询问题时,还可以查询评论表并检索以当前问题 objectID 作为“父级”的所有评论。

您在评论中提到

该解析不是关系性的。 Parse 基于 MongoDB,它是一个 NoSQL 数据库。但是,您可以“通过引用”建立关系。

http://docs.mongodb.org/manual/core/data-modeling-introduction/

检索问题和评论

//First get the question
var Question = Parse.Object.extend("Question");
var query = new Parse.Query(Question);
query.equalTo("questionID", questionID);
query.find({
  success: function(results) {
     //You will have to check results and see if it returns an array
     //If it does you will need to use results[0].id to select 1st
     //element in array. The follow code is if results is an Object

     var JoinTable = Parse.Object.extend("JoinTable");
     var query = new Parse.Query(JoinTable);
     query.equalTo("parentID", questionID);
     query.find({
        success: function(results) {
        //this should return an array, loop through the array to get
        //all of the IDs then, pe## Heading ##rform a compound query with all of 
        //IDs

      },
     error: function(error) {
        alert("Error: " + error.code + " " + error.message);
     }
    });
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

但是,编写该代码非常烦人且没有必要,因此我在这里推荐选项二,说明如何做到这一点

//First get the question
var Question = Parse.Object.extend("Question");
var query = new Parse.Query(Question);
query.equalTo("questionID", questionID);
query.find({
  success: function(results) {
     //You will have to check results and see if it returns an array
     //If it does you will need to use results[0].id to select 1st
     //element in array. The follow code is if results is an Object

    var questionID = results.id;

     var Comments = Parse.Object.extend("Comments");
     var query = new Parse.Query(Comments);
     query.equalTo("parentID", questionID);
     query.find({
        success: function(results) {
         //This returns your comments to be shared with your view

      },
     error: function(error) {
        alert("Error: " + error.code + " " + error.message);
     }
    });
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

您会注意到,我停止在 commentID 上的 forEach 循环中编写实际代码,因为我意识到这是多么不必要。希望这会有所帮助。

关于javascript - 如何在Parse上保存具有不同ACL的Parse对象的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928946/

相关文章:

访问预定义数组时代码崩溃

c - 如何在 C 中使错误的指针算术更加明显?

javascript - 将 token 存储到 JS 本地存储安全吗?

javascript - 在 jQuery 中切换类

c - 如何将项目扫描到 void* 数组中?

api - 使用 Arduino 和 GSM 盾以及 Parse REST API 将数据上传到 Parse.com

java - 解析错误 com.parse.ParseRequest$ParseRequestException : i/o failure

javascript - 如何从 $http.get 请求中删除 header 并将 "application/json"和 "text/plain"添加到 "Accept"

javascript - 使用 ajax REST 使用从 SharePoint 列表中提取的数据进行 jQuery 自动完成

android - 如何从 Android 应用程序中删除解析用户?