如何在Parse上使用父对象的不同ACL保存指针?

时间:2022-10-04 10:11:20

I have a very hard issue here; I looked the Parse.com js SDK at least three times now but can't figure this out.

我这里有一个很难的问题;我现在至少看了三次Parse.com js SDK,但是无法解决这个问题。

I have a Question Class with text, user, comments. I have a Comment Class with text, user.

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

Comments are placed in an array of pointers to the Question Class.

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

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

Here everything fine. Now another user log's in the app and posts a comment

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

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... 

it says user don't have the right to write on the question object which is normal but how can the comment be saved if I don't save the question with the new array?

它说用户没有权利在问题对象上写这是正常的,但如果我不用新数组保存问题,如何保存注释?

1 个解决方案

#1


1  

OPTION 1: JOIN TABLE

选项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
});

Now, when you load a question just do a query on the join table by the questionID which will give you all commentIDs associated with the question. Then query the comments table with that commentID, load, and display those results.

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

OPTION 2: SAVE QUESTION ID WITH COMMENT

选项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;
});;

Now, when you query for the Questions, also query the comments table and retrieve all comments with the current questions objectID as their 'parent'.

现在,当您查询问题时,还要查询注释表并检索所有注释,并将当前问题objectID作为其“父”。

You Mentioned in Comment

你在评论中提到过

That parse is not relational. Parse is based on MongoDB which is a NoSQL database. However, you can do relations "by reference."

那个解析不是关系。 Parse基于MongoDB,它是一个NoSQL数据库。但是,您可以“通过引用”进行关系。

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

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

TO RETRIEVE BOTH QUESTIONS AND COMMENTS

要回顾两个问题和评论

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

HOWEVER, WRITING THAT CODE WAS INCREDIBLY ANNOYING AND UNNECESSARY SO I RECOMMEND OPTION TWO HERE IS HOW TO DO THAT

然而,写这个代码令人难以置信的烦恼和不必要所以我推荐选项两个在这里如何做

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

You will notice I stopped writing out the actual code at the forEach loop on the commentIDs because I realized how unnecessary it was. Hopefully this helps.

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

#1


1  

OPTION 1: JOIN TABLE

选项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
});

Now, when you load a question just do a query on the join table by the questionID which will give you all commentIDs associated with the question. Then query the comments table with that commentID, load, and display those results.

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

OPTION 2: SAVE QUESTION ID WITH COMMENT

选项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;
});;

Now, when you query for the Questions, also query the comments table and retrieve all comments with the current questions objectID as their 'parent'.

现在,当您查询问题时,还要查询注释表并检索所有注释,并将当前问题objectID作为其“父”。

You Mentioned in Comment

你在评论中提到过

That parse is not relational. Parse is based on MongoDB which is a NoSQL database. However, you can do relations "by reference."

那个解析不是关系。 Parse基于MongoDB,它是一个NoSQL数据库。但是,您可以“通过引用”进行关系。

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

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

TO RETRIEVE BOTH QUESTIONS AND COMMENTS

要回顾两个问题和评论

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

HOWEVER, WRITING THAT CODE WAS INCREDIBLY ANNOYING AND UNNECESSARY SO I RECOMMEND OPTION TWO HERE IS HOW TO DO THAT

然而,写这个代码令人难以置信的烦恼和不必要所以我推荐选项两个在这里如何做

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

You will notice I stopped writing out the actual code at the forEach loop on the commentIDs because I realized how unnecessary it was. Hopefully this helps.

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