I am confused on how to update a user from cloud code. If someone can help me out with getting my mind right about this I'd appreciate it.
The cloud code I will use for this example is:
cloud code
Parse.Cloud.define("like", function(request, response) {
var userID = request.params.userID;
var User = Parse.User();
user = new User ({ objectId: userID });
user.increment("likes");
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
called from iOS
[PFCloud callFunctionInBackground:#"like" withParameters:#{#"userID": self.selectedFriendID} block:^(id object, NSError *error) {
}];
Questions
user = new User ({ objectId: userID });
1) is the "new" in the code above creating a "brand new" user, or is it "grabbing" an existing user at the objectId so that it can be updated on user.save?
2) if the latter is correct, than is it possible to "grab" a user from a column other than the objectId column, and maybe instead grab from the userID column?
eg:
user = new User ({ userID : userID });
This line:
user = new User ({ objectId: userID });
Creates an instance of an object with a known ID, it only works with objectId. Any changes you make to that object are persisted, but no other data is changed (e.g. you won't accidentally blank out the other columns).
If instead you wanted to get a user by email you would have to do a query:
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('email', userEmail);
userQuery.first().then(function(user) {
if (user) {
// do something with the user here
user.set('new_col', 'new text');
user.save().then(function() {
response.success();
});
} else {
// no match found, handle it or do response.error();
}
});
// the above code is async, don't put anything out here and expect it to work!
What I would do to retrieve the user in cloud code is query.get(request.params.userID.... I believe this returns the user object. I do not know if you can do this with other columns. There is lots of stuff in the cloud code docs about this, though. Here is my cloud code function for editing a user, if that helps:
Parse.Cloud.define("editUser", function(request, response) {
//var GameScore = Parse.Object.extend("SchoolHappening");
// Create a new instance of that class.
//var gameScore = new GameScore();
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.get(request.params.myUser, {
success: function(myUser) {
// The object was retrieved successfully.
myUser.set("cabinetPosition", request.params.myPosition);
// Save the user.
myUser.save(null, {
success: function(myUser) {
// The user was saved successfully.
response.success("Successfully updated user.");
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
response.error("Could not save changes to user.");
}
});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
});
Related
My insert is not working, I got error of Error :
Token.insert is not a function
var Token = module.exports = mongoose.model('tokens', tokenSchema);
//error
module.exports.saveToken = function(owner_id, token, callback){
console.log(owner_id,token);
Token.insert({"owner":owner_id,"token":token},callback);
}
//working
module.exports.getAllTokens = function(owner_id, callback){
Token.find({"owner":owner_id},callback);
}
Check this code example, it should work as You need.
I don's see here any non-understanding part.
Ask questions in comments, I can explain if don't understand.
var tokenSchema = mongoose.Schema({
owner: {
type: 'String',
required: true,
index: {
unique: true
}
},
token: {
type: ['String'],
default: []
}
});
var Token = module.exports = mongoose.model('tokens', tokenSchema);
//save token, if token document exist so push it in token array and save
module.exports.saveToken = function(owner_id, token, callback){
Token
.findOne({owner: owner_id})
.exec(function(err, tokenDocument) {
if(tokenDocument) {
if(tokenDocument.token.indexOf(token) > -1) { // found that token already exist in document token array
return callback(null, tokenDocument); // don't do anything and return to callback existing tokenDocument
}
tokenDocument.token.push(token);
tokenDocument.save(callback);
return; // don't go down, cuz we already have a token document
}
new Token({owner: owner_id, token: [token]}).save(callback); // create new token document with single token in token array
});
}
//get all tokens by owner_id
module.exports.getAllTokens = function(owner_id, callback){
Token
.findOne({owner: owner_id})
.exec(function(err, tokenDocument) {
callback(err, tokenDocument.token);
});
}
You can use:
let newvalue = new Token({"owner":owner_id,"token":token});
newvalue.save();
Here i defined what is the problem that why they did not find the insertMany
generally, we create an instance to save a document in DB like this :
const instance = new Model(data);
After then we hit the save query
instance.save(data).then().catch()
But when we talk about the insertMany Section, No need to create any instance. you can insert all entries directly via Model
eg.
Modal.insertMany(data).then().catch()
I am new to meteor and autoform and am trying to get a form to insert into mongo. No matter what I have changed it just will not work. I have no idea what to try next.
I have removed insecure and autopublish. Attached is a link to my .js and my html file.
I have setup a scheme, gotten the form to show on the html perfectly. now when I hit submit nothing is being put into mongo. I have allow rules setup. I have a ton of console.logs displaying and they all trigger and follow along as if the post was successful. In fact in the onSuccess I get a document number yet there is nothing in my DB.
Any help here is greatly appreciated. I know it must be something small here but I have racked my brain for hours of endless searching.
.js file without the scheme. Full js file in the plnkr link
if (Meteor.isClient) {
// ********************************************************
// *** Creating the database scheme for the customer account
// ********************************************************
customers = new Mongo.Collection("customer");
AutoForm.debug();
var postHooks = {
before: {
insert: function(doc) {
console.log("Getting to posting hooks");
if(Meteor.userId()){
doc.createdUser = Meteor.userId();
doc.createdDate = Date();
console.log("Got to the insert before hook!");
}
return doc;
}
},
after: {
// Replace `formType` with the form `type` attribute to which this hook applies
insert: function(error, result) {
console.log("Getting to the after insert function");
console.log(error);
console.log(result);
console.log("New Document ID is " + this.docId);
}
},
onSuccess: function(formType, result) {
console.log("Getting to the insert sucess area");
console.log(result);
},
onError: function(formType, error) {
console.log(error);
}
}
AutoForm.addHooks('insertCustomer', postHooks);
Template.customerTemplate.helpers({
showLoginError: function(){
return showCustomerSaveError;
}
});
}
if (Meteor.isServer) {
customers = new Mongo.Collection("customer");
customers.allow({
insert: function (userId, doc) {
console.log("Getting to the insert server call");
// the user must be logged in
return !! userId;
},
update: function (userId, doc, fields, modifier) {
// can only change your own documents
return doc.owner === userId;
},
remove: function (userId, doc) {
// can only remove your own documents
return doc.owner === userId;
},
fetch: ['owner']
});
}
http://plnkr.co/edit/5pM0Co9luGLIBBNKP5YA
You're adding the file to the database but not publishing it.
Run meteor add autopublish or check in MongoDB itself to see the document.
I have an App using Parse.com as a backend and an external site that acts as my payment gateway. Upon receiving the customer/subscription webhook data from Stripe I wish to lookup the users email so I can then run a Cloud Code function and change their user status to 'paid'
My webhook receiver is:
Parse.Cloud.define("update_user", function(request, response) {
var data = request.params["data"]
var customer = data.object.customer;
response.success'Working' + request);
});
And I am able to get an email back from stripe from the customer ID using:
Parse.Cloud.define("pay", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);
console.log(JSON.stringify(request.params));
Stripe.Customers.retrieve(
customerId, {
success:function(results) {
console.log(results["email"]);
// alert(results["email"]);
response.success(results);
},
error:function(error) {
response.error("Error:" +error);
}
}
);
});
I need help turning this into a complete function that is run on receipt of every webhook from Stripe. I am also struggling with options for fallback if this does not work for whatever reason.
EDIT
Taking parts of the first answer and I now have:
Parse.Cloud.define("update_user", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);
var data = request.params["data"]
var customerId = data.object.customer;
get_stripe_customer(customerId, 100).then(function(stripeResponse) {
response.success(stripeResponse);
}, function(error) {
response.error(error);
});
});
function get_stripe_customer (customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
return Stripe.Customers.retrieve(
customerId, {
success:function(results) {
console.log(results["email"]);
},
error:function(error) {
}
}
);
};
My knowledge is really falling down on the Promise side of things and also the callback (success:, error, request response) etc further reading would be appreciated.
This is now working
Out of interest I did this:
Parse.Cloud.define("update_user", function(request, response) {
var data = request.params["data"]
var customerId = data.object.customer;
get_stripe_customer(customerId, 100).then(function(stripeResponse) {
return set_user_status(username, stripeResponse);
}).then(function(username) {
response.success(username);
}, function(error) {
response.error(error);
});
});
function get_stripe_customer (customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
return Stripe.Customers.retrieve(
customerId, {
success:function(results) {
// console.log(results["email"]);
},
error:function(error) {
}
}
);
};
function set_user_status(stripeResponse) {
Parse.Cloud.useMasterKey();
var emailquery = new Parse.Query(Parse.User);
emailquery.equalTo("username", stripeResponse['email']); // find all the women
return emailquery.first({
success: function(results) {
alert('running set_user_status success');
var user = results;
user.set("tier", "paid");
user.save();
},
error:function(error) {
console.log('error finding user');
}
});
};
open to improvements...
EDIT - I (#danh) cleaned it up a bit. A few notes:
used promises throughout. much easier to read and handle errors
get_stripe_customer requires only one param (that 100 was my idea to charge $100)
set_user_status appears to need only user email as param, which apparently is in the stripeResponse
set_user_status returns a promise to save the user. that will be fulfilled with the user object, not the username
be sure you're clear on how to identify the user. stripe apparently provides email address, but in your user query (in set_user_status) you compare email to "username". some systems set username == email. make sure yours does or change that query.
Parse.Cloud.define("update_user", function(request, response) {
var data = request.params["data"]
var customerId = data.object.customer;
get_stripe_customer(customerId).then(function(stripeResponse) {
var email = stripeResponse.email;
return set_user_status(email);
}).then(function(user) {
response.success(user);
}, function(error) {
response.error(error);
});
});
function get_stripe_customer(customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
return Stripe.Customers.retrieve(customerId).then(function(results) {
// console.log(results["email"]);
return results;
});
};
function set_user_status(email) {
Parse.Cloud.useMasterKey();
var emailquery = new Parse.Query(Parse.User);
emailquery.equalTo("username", email); // find all the women
return emailquery.first().then(function(user) {
user.set("tier", "paid");
return user.save();
}, function(error) {
console.log('error finding user ' + error.message);
return error;
});
}
Did a quick skim of the docs pertaining to stripe, and it looks like the steps are: (1) make a stripe REST-api call from your client side to get a token, (2) pass that token to a cloud function, (3) call stripe from the parse cloud to finish paying. I understand that you'd like to include a (4) fourth step wherein the transaction is recorded in the data for the paying user.
From the client (assuming a JS client):
var token = // we've retrieved this from Stripe's REST api
Parse.Cloud.run("pay", { stripeToken: token }).then(function(result) {
// success
}, function(error) {
// error
});
On the server:
Parse.Cloud.define("pay", function(request, response) {
var user = request.user;
var stripeToken = request.params.stripeToken;
payStripeWithToken(stripeToken, 100).then(function(stripeResponse) {
return updateUserWithStripeResult(user, stripeResponse);
}).then(function(user) {
response.success(user);
}, function(error) {
response.error(error);
});
});
Now we need only to build promise-returning functions called payStripeWithToken and updateUserWithStripeResult.
// return a promise to pay stripe per their api
function payStripeWithToken(stripeToken, dollarAmt) {
Stripe.initialize(STRIPE_SECRET_KEY); // didn't see this in the docs, borrowed from your code
return Stripe.Charges.create({
amount: dollarAmt * 10, // expressed in cents
currency: "usd",
card: stripeToken //the token id should be sent from the client
});
// caller does the success/error handling
}
// return a promise to update user with stripeResponse
function updateUserWithStripeResult(user, stripeResponse) {
var transactionId = // dig this out of the stripeResponse if you need it
user.set("paid", true);
user.set("transactionId", transactionId);
return user.save();
}
Any ideas why this isn't working? It comes back with the success message but doesn't actually update anything.
I'm adding an FB user using Parse but since it doesn't have a func to add the username and email trying to do it this way. Any help much appreciated.
JS
Parse.Cloud.run("test", { objectId: "Q8XRUcL22N", name: "Rich", email: "rich#gmail.com"}, {
success: function(results) {
console.log(results);
},
error: function(error) {
console.log(error);
}
});
CLOUD CODE:
Parse.Cloud.define("test", function (request, response) {
Parse.Cloud.useMasterKey();
var uid;
var query = new Parse.Query("_User");
query.count({
success: function (results) {
uid = parseInt(results);
},
error: function () {
response.error("UID lookup failed");
}
});
var query = new Parse.Query(Parse.User);
query.equalTo("objectId", request.params.objectId);
query.first({
success: function (object) {
object.set("username", request.params.name);
object.set("email", request.params.email);
object.set("uid", uid);
var acl = new Parse.ACL();
acl.setPublicWriteAccess(false);
acl.setPublicReadAccess(false);
object.setACL(acl);
object.save();
response.success("Success Message");
},
error: function (error) {
response.error("Error Message");
}
});
});
Thanks
Calling success() or error() on the response halts whatever is underway at the time, including the save() on the user.
Also, it looks like you want to record in the user a count of users at the time the ACL is set. Getting the count must also be serialized with the other operations. (Also, please note that count is only good as an id "uid" to the extent the user count never goes down. What's wrong with the parse objects's id as an id?).
Most of the parse functions return promises, and using them is the only way to not go nuts trying to nest callbacks. So...
Parse.Cloud.define("test", function(request, response) {
Parse.Cloud.useMasterKey();
var uid;
var query = new Parse.Query(Parse.User);
query.count().then(function(count) {
uid = count;
return query.get(request.params.objectId);
}).then(function(object) {
object.set("username", request.params.name);
object.set("email", request.params.email);
object.set("uid", uid);
var acl = new Parse.ACL();
acl.setPublicWriteAccess(false);
acl.setPublicReadAccess(false);
object.setACL(acl);
return object.save();
}).then(function (object) {
response.success(object);
},function(error) {
response.error("Error Message");
});
});
I have an Angular service that takes in a roleId and userId and assigns the user to that role and make a pointer in User to that role.
app.service('CRUD', function () {
this.addUserToRole = function (roleId, userId) {
// first we have to find the role we're adding to
var query = new Parse.Query(Parse.Role);
return query.get(roleId, {
success: function (role) {
// then add the user to it
var Database = Parse.Object.extend("User");
var query = new Parse.Query(Database);
console.log(role);
return query.get(userId, {
success: function (user) {
console.log(user);
role.getUsers().add(user);
role.save();
// now we need to tell the user that he has this role
console.log(user);
user.attributes.role.add(role);
user.save();
return user;
},
error: function (err) {
return err;
}
});
},
error: function (err) {
console.log(err);
}
});
}
});
I'm getting {"code":206,"error":"Parse::UserCannotBeAlteredWithoutSessionError"} on user.save();
After some research, I arrived at this website. He uses this code snippet as a JS SDK example:
Parse.Cloud.run('modifyUser', { username: 'userA' }, {
success: function(status) {
// the user was updated successfully
},
error: function(error) {
// error
}
});
and mentions something about a useMasterKey() function.
I'm still unsure how to fix this error.
Add
Parse.Cloud.useMasterKey();
at the beginning of your function.
Set it up as a background job. That is the code snip you found I think and a simpler far more secure means of fondling users and roles
https://parse.com/docs/cloud_code_guide#jobs