How to create and save object from cloud code (parse server)
I need to create a cloud function after user registration that will create an object in path/class "publicUserInfo" with objectId same as the user's Id with given fields. I've tried to put together some code of the already answered questions here and there but I haven't seen a simple example.
My code so far:
Parse.Cloud.afterSave(Parse.User, function(request) {
console.log("Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :");
var id = request.object.id;
var date = new Date();
var userObject = Parse.Object.extend("publicUserInfo");
var userQuery = new Parse.Query(userObject);
userQuery.equalTo("objectId", id);
userQuery.first({
useMasterKey: true,
success:function(userData){
userData.set("nick", id);
userData.set("nickch", date);
userData.set("avatar", "1");
userData.set("avatarch", date);
userData.set("points", 0);
userData.set("state", true);
userData.set("lastSeen", date);
userData.save(null, { useMasterKey: true });
},
error: function(error){
console.log(error);
response.error(error);
}
});
});
I think the code is searching for an object first and if found it inserts the data. I might be wrong but I need it to save the data in publicUserInfo -> id same as the user's, the data is not there hence no need to be searched for, I must create it. Any ideas?
javascript parse.com cloud-code
add a comment |
I need to create a cloud function after user registration that will create an object in path/class "publicUserInfo" with objectId same as the user's Id with given fields. I've tried to put together some code of the already answered questions here and there but I haven't seen a simple example.
My code so far:
Parse.Cloud.afterSave(Parse.User, function(request) {
console.log("Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :");
var id = request.object.id;
var date = new Date();
var userObject = Parse.Object.extend("publicUserInfo");
var userQuery = new Parse.Query(userObject);
userQuery.equalTo("objectId", id);
userQuery.first({
useMasterKey: true,
success:function(userData){
userData.set("nick", id);
userData.set("nickch", date);
userData.set("avatar", "1");
userData.set("avatarch", date);
userData.set("points", 0);
userData.set("state", true);
userData.set("lastSeen", date);
userData.save(null, { useMasterKey: true });
},
error: function(error){
console.log(error);
response.error(error);
}
});
});
I think the code is searching for an object first and if found it inserts the data. I might be wrong but I need it to save the data in publicUserInfo -> id same as the user's, the data is not there hence no need to be searched for, I must create it. Any ideas?
javascript parse.com cloud-code
add a comment |
I need to create a cloud function after user registration that will create an object in path/class "publicUserInfo" with objectId same as the user's Id with given fields. I've tried to put together some code of the already answered questions here and there but I haven't seen a simple example.
My code so far:
Parse.Cloud.afterSave(Parse.User, function(request) {
console.log("Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :");
var id = request.object.id;
var date = new Date();
var userObject = Parse.Object.extend("publicUserInfo");
var userQuery = new Parse.Query(userObject);
userQuery.equalTo("objectId", id);
userQuery.first({
useMasterKey: true,
success:function(userData){
userData.set("nick", id);
userData.set("nickch", date);
userData.set("avatar", "1");
userData.set("avatarch", date);
userData.set("points", 0);
userData.set("state", true);
userData.set("lastSeen", date);
userData.save(null, { useMasterKey: true });
},
error: function(error){
console.log(error);
response.error(error);
}
});
});
I think the code is searching for an object first and if found it inserts the data. I might be wrong but I need it to save the data in publicUserInfo -> id same as the user's, the data is not there hence no need to be searched for, I must create it. Any ideas?
javascript parse.com cloud-code
I need to create a cloud function after user registration that will create an object in path/class "publicUserInfo" with objectId same as the user's Id with given fields. I've tried to put together some code of the already answered questions here and there but I haven't seen a simple example.
My code so far:
Parse.Cloud.afterSave(Parse.User, function(request) {
console.log("Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :");
var id = request.object.id;
var date = new Date();
var userObject = Parse.Object.extend("publicUserInfo");
var userQuery = new Parse.Query(userObject);
userQuery.equalTo("objectId", id);
userQuery.first({
useMasterKey: true,
success:function(userData){
userData.set("nick", id);
userData.set("nickch", date);
userData.set("avatar", "1");
userData.set("avatarch", date);
userData.set("points", 0);
userData.set("state", true);
userData.set("lastSeen", date);
userData.save(null, { useMasterKey: true });
},
error: function(error){
console.log(error);
response.error(error);
}
});
});
I think the code is searching for an object first and if found it inserts the data. I might be wrong but I need it to save the data in publicUserInfo -> id same as the user's, the data is not there hence no need to be searched for, I must create it. Any ideas?
javascript parse.com cloud-code
javascript parse.com cloud-code
asked Jan 2 at 22:52
A. NewbieA. Newbie
45117
45117
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you cannot set the id, and you shouldn't need to. Instead, make a pointer to the user that the public user info belongs to.
Here's my version of the code with a unit test to make sure that it works as expected. Note that I am using modern javascript. You should be using either node 8 or 10, my code will run with that.
const makePublicUser = function makePublicUser(request) {
// use destructuring to get request objects
const { object: user, log } = request;
log.info('Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :');
const date = new Date();
const userQuery = new Parse.Query('publicUserInfo');
userQuery.equalTo('user', user);
// could use async/await, but will do with promises
return userQuery.first({ useMasterKey: true }) // shouldn't need the master here
.then((result) => {
const userData = result || new Parse.Object('publicUserInfo', { user });
userData.set('nick', id);
userData.set('nickch', date);
userData.set('avatar', '1');
userData.set('avatarch', date);
userData.set('points', 0);
userData.set('state', true);
userData.set('lastSeen', date);
return userData.save(null, { useMasterKey: true });
})
.catch(log.error.bind(log));
};
describe('StackOverflow', function () {
beforeAll(function () {
Parse.Cloud.afterSave(Parse.User, makePublicUser);
});
it('should create a publicUserInfo', async function () {
const user = new Parse.User({ username: 'foo', password: 'bar' });
await user.save();
const publicUserInfo = await new Parse.Query('publicUserInfo')
.equalTo('user', user)
.first();
expect(publicUserInfo).not.toBeNull();
});
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014214%2fhow-to-create-and-save-object-from-cloud-code-parse-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you cannot set the id, and you shouldn't need to. Instead, make a pointer to the user that the public user info belongs to.
Here's my version of the code with a unit test to make sure that it works as expected. Note that I am using modern javascript. You should be using either node 8 or 10, my code will run with that.
const makePublicUser = function makePublicUser(request) {
// use destructuring to get request objects
const { object: user, log } = request;
log.info('Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :');
const date = new Date();
const userQuery = new Parse.Query('publicUserInfo');
userQuery.equalTo('user', user);
// could use async/await, but will do with promises
return userQuery.first({ useMasterKey: true }) // shouldn't need the master here
.then((result) => {
const userData = result || new Parse.Object('publicUserInfo', { user });
userData.set('nick', id);
userData.set('nickch', date);
userData.set('avatar', '1');
userData.set('avatarch', date);
userData.set('points', 0);
userData.set('state', true);
userData.set('lastSeen', date);
return userData.save(null, { useMasterKey: true });
})
.catch(log.error.bind(log));
};
describe('StackOverflow', function () {
beforeAll(function () {
Parse.Cloud.afterSave(Parse.User, makePublicUser);
});
it('should create a publicUserInfo', async function () {
const user = new Parse.User({ username: 'foo', password: 'bar' });
await user.save();
const publicUserInfo = await new Parse.Query('publicUserInfo')
.equalTo('user', user)
.first();
expect(publicUserInfo).not.toBeNull();
});
});
add a comment |
you cannot set the id, and you shouldn't need to. Instead, make a pointer to the user that the public user info belongs to.
Here's my version of the code with a unit test to make sure that it works as expected. Note that I am using modern javascript. You should be using either node 8 or 10, my code will run with that.
const makePublicUser = function makePublicUser(request) {
// use destructuring to get request objects
const { object: user, log } = request;
log.info('Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :');
const date = new Date();
const userQuery = new Parse.Query('publicUserInfo');
userQuery.equalTo('user', user);
// could use async/await, but will do with promises
return userQuery.first({ useMasterKey: true }) // shouldn't need the master here
.then((result) => {
const userData = result || new Parse.Object('publicUserInfo', { user });
userData.set('nick', id);
userData.set('nickch', date);
userData.set('avatar', '1');
userData.set('avatarch', date);
userData.set('points', 0);
userData.set('state', true);
userData.set('lastSeen', date);
return userData.save(null, { useMasterKey: true });
})
.catch(log.error.bind(log));
};
describe('StackOverflow', function () {
beforeAll(function () {
Parse.Cloud.afterSave(Parse.User, makePublicUser);
});
it('should create a publicUserInfo', async function () {
const user = new Parse.User({ username: 'foo', password: 'bar' });
await user.save();
const publicUserInfo = await new Parse.Query('publicUserInfo')
.equalTo('user', user)
.first();
expect(publicUserInfo).not.toBeNull();
});
});
add a comment |
you cannot set the id, and you shouldn't need to. Instead, make a pointer to the user that the public user info belongs to.
Here's my version of the code with a unit test to make sure that it works as expected. Note that I am using modern javascript. You should be using either node 8 or 10, my code will run with that.
const makePublicUser = function makePublicUser(request) {
// use destructuring to get request objects
const { object: user, log } = request;
log.info('Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :');
const date = new Date();
const userQuery = new Parse.Query('publicUserInfo');
userQuery.equalTo('user', user);
// could use async/await, but will do with promises
return userQuery.first({ useMasterKey: true }) // shouldn't need the master here
.then((result) => {
const userData = result || new Parse.Object('publicUserInfo', { user });
userData.set('nick', id);
userData.set('nickch', date);
userData.set('avatar', '1');
userData.set('avatarch', date);
userData.set('points', 0);
userData.set('state', true);
userData.set('lastSeen', date);
return userData.save(null, { useMasterKey: true });
})
.catch(log.error.bind(log));
};
describe('StackOverflow', function () {
beforeAll(function () {
Parse.Cloud.afterSave(Parse.User, makePublicUser);
});
it('should create a publicUserInfo', async function () {
const user = new Parse.User({ username: 'foo', password: 'bar' });
await user.save();
const publicUserInfo = await new Parse.Query('publicUserInfo')
.equalTo('user', user)
.first();
expect(publicUserInfo).not.toBeNull();
});
});
you cannot set the id, and you shouldn't need to. Instead, make a pointer to the user that the public user info belongs to.
Here's my version of the code with a unit test to make sure that it works as expected. Note that I am using modern javascript. You should be using either node 8 or 10, my code will run with that.
const makePublicUser = function makePublicUser(request) {
// use destructuring to get request objects
const { object: user, log } = request;
log.info('Custom log -> Parse.Cloud.afterSave(Parse.User, function(request){} :');
const date = new Date();
const userQuery = new Parse.Query('publicUserInfo');
userQuery.equalTo('user', user);
// could use async/await, but will do with promises
return userQuery.first({ useMasterKey: true }) // shouldn't need the master here
.then((result) => {
const userData = result || new Parse.Object('publicUserInfo', { user });
userData.set('nick', id);
userData.set('nickch', date);
userData.set('avatar', '1');
userData.set('avatarch', date);
userData.set('points', 0);
userData.set('state', true);
userData.set('lastSeen', date);
return userData.save(null, { useMasterKey: true });
})
.catch(log.error.bind(log));
};
describe('StackOverflow', function () {
beforeAll(function () {
Parse.Cloud.afterSave(Parse.User, makePublicUser);
});
it('should create a publicUserInfo', async function () {
const user = new Parse.User({ username: 'foo', password: 'bar' });
await user.save();
const publicUserInfo = await new Parse.Query('publicUserInfo')
.equalTo('user', user)
.first();
expect(publicUserInfo).not.toBeNull();
});
});
answered Jan 7 at 23:40
Arthur CinaderArthur Cinader
839517
839517
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014214%2fhow-to-create-and-save-object-from-cloud-code-parse-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown