I have a Parse Class Group and there is a parse relation field in it, called people (users, who are in this group). I am implementing a afterSave on "Group". I want to notify users, who are just added by admin into this group.
How do i do that ?
Parse.Cloud.afterSave(Group, function(request){
Parse.Cloud.useMasterKey();
var group = request.object;
var relation = group.relation("people");
//How to get users that are added on this save.
});
To find the new records being added to a relation, you need to inspect the relationsToAdd property of a Relation (its an array):
var newRecords = request.object.op("people").relationsToAdd;
I know this works in beforeSave but have not used it in afterSave tirggers
Related
I'm working with Xamarin in Visual Studio.
I'm utilizing Parse (via SashiDo.com) and I'm trying to create a relation between my Users and the ParseObjects in a table called called Dispatch, like so:
//Make the new Dispatch object
var parseDispatch = new Parse.ParseObject("Dispatch");
//Save it
await parseDispatch.SaveAsync();
//...Setting various properties on the Dispatch object...
//Get a list of users (via another method)
IEnumerable<ParseUser> usersToLink = UsersToLinkToDispatch(); //And have verified elsewhere that this indeed returns a collection of ParseUsers
//Go through the users collection
usersToLink.ToList().ForEach( async (user) => {
//Get or create the dispatches-tracking relation for this user
var dispatchObjectRelation = parseDispatch.GetRelation<ParseObject>("DispatchesTracked");
//Add the current user to that tracker relation
dispatchObjectRelation.Add(user);
//save the dispatch to update the relation
await parseDispatch.SaveAsync();
});
So, when I go to inspect my tables in SashiDo, if I look at the Dispatches table, I see a proper-looking relational link, and if I click on that link, I see the list of linked Users. So far so good, right?
But if I look at the Users table, while there also seems to be a proper-looking relational link, when I click on it I do not see a list of linked Dispatches.
Is this expected behavior, or is this apparent one-way-ness of the relational link an error?
I have a Parse Object Event it contain a key attendees, a Parse Relation object. My question is how to retrieve all event where current user is in the attendees relation ? my current code is :
var query = new Parse.Query(Event).equalTo('attendees',currentUser)
query.find({
success:function(list){
}
})
Your query seems Ok, try to verify your data in DB.
cmd from mongo shell
db.getCollection('_Join:attendees:Event').find({'relatedId':currentuserId})
I have the same case that is used in the Parse documentation for many-to-many relations using a join table.
In my case I am fetching a list of users by a simple query, but what I need is to know if current user following the user in the list, meaning I want to add a button to the list of users that allows the current user to follow or unfollow users in the list based on their following status.
Is there any chance that I can get this info with one query?
this will help you. see Relational Queries
var following = Parse.Object.extend("Following"); //Following (ParseObject)
var currentUser = Parse.User.current();
var innerQuery = new Parse.Query(following);
innerQuery.exists("status");
var query = new Parse.Query(currentUser);
query.matchesQuery("follow", innerQuery); //follow is pointer type
query.find({
success: function(comments) {
}
});
im working on an Android App.
I have a custom class which has relations with TWO ParseUsers and other fields. As suggested by the docs, I used an array (with key "usersArray") to store the pointers for the two ParseUsers, because I want to be able to use "include" to include the users when i query my custom class. I can create a new object and save it successfully.
//My custom parse class:
CustomObject customObject = new CustomObject();
ArrayList<ParseUser> users = new ArrayList<ParseUser>();
users.add(ParseUser.getCurrentUser());
users.add(anotherUser);
customObject.put("usersArray", users);
//I also store other variable which i would like to update later
customObject.put("otherVariable",false);
customObject.saveInBackground();
Also, i can query successfully with:
ParseQuery<CustomObject> query = CustomObject.getQuery();
query.whereEqualTo("usersArray", ParseUser.getCurrentUser());
query.whereEqualTo("usersArray", anotherUser);
query.include("usersArray");
query.findInBackground( .... );
My problem is when trying to UPDATE one of those CustomObject.
So after retrieving the CustomObject with the previous query, if I try to change the value of the "otherVariable" to true and save the object, I am getting a UserCannotBeAlteredWithoutSessionError or java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated exceptions.
CustomObject customObject = customObject.get(0); //From the query
customObject.put("otherVariable", true);
customObject.saveInBackground(); // EXCEPTION
I can see this is somehow related to the fact im trying to update an object which contains a pointer to a ParseUser. But im NOT modifying the user, i just want to update one of the fields of the CustomObject.
¿There is any way to solve this problem?
Maybe late but Parse users have ACL of public read and private write so you should do users.isAuthenticated() to check if its true or false.
If false then login with the user and retry. Note: you cannot edit info on two users at the same time without logging out and relogging in.
Another thing you can do is use Roles and define an admin role by using ACL which can write over all users.
here is my problem
i have three schema
var UserSchema = new Schema({
username:String,
email:String,
hashed_password:String,
salt:String,
shop_id:{type:Schema.Types.ObjectId,ref:'Shop'},
})
var ShopSchema = new Schema({
owner_id:{type:Schema.Types.ObjectId,ref:'User'},
owner_real_id:String,
owner_real_name:String,
owner_real_location:String,
shop_name:String,
sell_product_ids:[Schema.Types.ObjectId],
})
var ProductSchema = new Schema({
})
it is necessary to sign up the userschema to use the app, but unnecessary to sign up the shopschema unless the user want to sell some stuff. however when the user do sign up the shopschema i need to update the userschema with the shop's _id,
so here is what i did
create the document in shop collection
find the shops _id
update the user collection
as u can see i query the datebase three times,so i was wondering if this can be done in one query in order to save time like
Shop.create(regist_data,function(){
//update the user collection here
})
Just in case u wondering why i need this, its becase i use 'passport' to log user in, and i want to acess the ProductShcema by shop's _id in the req.user, otherwise every time i want to acess the ProductShcema i nend to find the shop's _id and then get the product that belong to the shop's _id.
any way if u have better solution,please let me know.thanx!!!
sorry i think i should've read the mongoose doc more carefully
here is what i figure out
Shop.create(regist_data,function(err,shop){
console.log('shop = '+shop);
User.findByIdAndUpdate(req.user.id,{ $set: { shop_id: shop._id }},{new:true},function(err,data){
if(err){
console.log(err)
}
console.log('new user = '+data);
})
})
it create the shop document and update the users collection , it works for me.