parse-server updating an existing user field? - javascript

What I am trying to do: I am using parse server with javaScript SDK to change a field value for an already existing user.
The code that i am using
var query = new Parse.Query(Parse.User);
query.equalTo("username", "someUserName");
query.find({
success: function(users){
console.log("user found", users);
users.set("name", "new name");
users.save(null,{
success: function(updated){
//worked
},
error: function(error, updated){
//didn't work
}
});
},
error: function(error, users){
console.error("error at querying: ", error);
}
});
The issue : this does not update the name field value.
What i have discovered from researching this issue
: seems like i have to utilize the sessioToken or/and masterKey like this(show below) but that also did not work.
users.save(null,{useMasterKey:true}{
success: function(updated){
//worked
}
I am quite confused at the moment, any help would be appreciated.

Your master key or session token should be the first parameter.Have a look at the Parse.Object.save API
https://www.parse.com/docs/js/api/classes/Parse.Object.html
Try something like this
users.save({useMasterKey:true}, {
success: function(updated){
//worked
}
error: function(err){
}
});

Related

How can i get result's body and status with jQuery

I've been developing a simple RESTful API with NodeJS and Express. When the backend was done and operative, my next step was to make HTML forms to fill the database and consume the API. I decided that using jQuery to submit the data would be a nice idea to get some practice .
So basically I want to get from a $.post the body and status that my app's backend generates whenever it recieves a POST request. Here's the form's script:
$('#addcube').submit(function(event){
//Stop the default behaviour of the submit button
event.preventDefault();
//Get the input values
var $form = $(this),
postData = {
nombre: $form.find('input[name="nombre"]').val(),
brand: $form.find('input[name="brand"]').val(),
capas: $form.find('input[name="capas"]').val(),
kind: $form.find('input[name="kind"]').val()
},
url = $form.attr('action');
$.ajax({
url: url,
type: 'post',
data: JSON.stringify(postData),
contentType: "application/json",
done: function(cube, textStatus, jqxhr){
console.log(JSON.parse(cube));
},
fail: function(jqxhr, textStatus, errorThrown){
console.log(errorThrown.msg);
}
});
});
And here's the backend route for that post:
app.post('/api/cube', cubeController.addCubo);
Which is controlled by this script:
module.exports.addCubo = function(req, res){
var Cube = require('../models/cube');
console.log('POST');
try{
console.log(req.body);
var cubo = new Cube({
nombre : req.body.nombre,
brand : req.body.brand,
capas : req.body.capas,
kind : req.body.kind
});
cubo.save(function(err){
if(!err){
console.log('Nuevo cubo guardado.');
res.send(JSON.stringify(cubo));
res.status(200);
}else{
console.log('Error al guardar: '+err);
res.send('{"status":"400","msg":"bad_request"}');
res.status(400);
}
});
}catch(err){
res.send('{"status":"500","msg":"internal_server_error"}');
}
};
No body property exists at response, use . Set $.post() type to json, or use JSON.parse() at .done(). Also, if sending error, use .fail() to log messages
var send = $.post(url, {
nombre : name,
brand : marca,
capas : layers,
kind : tipo
}, "json"); // set expected response type to `"json"`
// Try to get the result
send.done(function(cube, textStatus) {
console.log(cube, textStatus)
});
// handle errors
send.fail(function(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown, errorThrown.msg)
});

Relational query with Parse

So I am trying to get the comments associated with a blog, Using Parse Javascript SDK
here is my code
var Blog = Parse.Object.extend("Blog");
var query = new Parse.Query(Blog);
query.get("tFlENZ3ege", {
success: function(blog) {
// Next, retrive comment based on blog
var commentQuery = new Parse.Query(Comment);
commentQuery.equalTo("parent", blog);
commentQuery.find({
success: function(comments) {
console.log("Success getting comments");
},
error: function(object, error) {
console.log(error);
}
});
},
error: function(object, error) {
}
});
I am able to retrieve the blog object based on the objectId.
However, I can't figure out how to get the associated comment objects

JSON - Unexpected end of input

I am not a JSON specialist and therefore I need help to solve a little problem.
When submitting a form, before the form closes, it returns 'Unexpected end of input'.
I have searched the Internet and this forum, I found nothing solving the problem. I hope you'll be able to help me sort it out.
So, to make it easier for you, what's going on?
The user requests a listing. He fills the form with a message and a price. The item_id is transmitted directly (and working as I get it working so far).
The form data are sent via JSON to a remote server (VPS) which un-json the data. However, in addition to the 'unexpected end of input', I got on my remote server 'Server response: 400 (Bad Request)'
I am almost sure it is due to this code and not the remote server one.
I have identified thanks to debug that the error is at the line :
JSON.parse(data);
$('#requestListingSubmit').click(function (evt) {
evt.preventDefault();
rL_form.hide();
rL_load.show();
rL_controls.hide();
rL_alert.empty();
var item_id = $(this).data('item-id');
var route = '/request';
$.ajax(
{
type: 'POST',
url: coreURL + route,
data:
{
'item_id': item_id,
'message': $('#message').val(),
'price': $('#price').val(),
},
success: function (data)
{
try
{
JSON.parse(data);
window.location = coreURL+'/account/listings?new';
}
catch(e)
{
rL_load.hide();
rL_form.show();
rL_controls.show();
rL_alert.alert(
{
type: 'danger',
message: 'Error: '+e.message
});
}
},
error: function (jqXHR, status, httperror) {
rL_load.hide();
rL_form.show();
rL_controls.show();
rL_alert.alert({
type: 'danger',
message: (jqXHR.responseJSON.message || httperror)
});
}
});
});
Thank you very much guys!

How to send the parse users objectId with the twilio module

I have been trying to get the Object id of the parse user, so that i can send it to the users phone number. The cloud code is done in javascript which i am not familiar with. I believe the problem is with my code. Here is what i have done so far:
var twilio = require("twilio");
twilio.initialize("myAccountSid","myAuthToken");
// Create the Cloud Function
Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// getting objectId from Parse
var query = new Parse.Query("objectId");
query.equalTo("username", request.params.number);
query.find({
success: function(httpResponse){
response.success("Code found");
},
error: function(httpResponse){
response.error("Code not found");
}
});
// Use the Twilio Cloud Module to send an SMS
twilio.sendSMS({
From: "myTwilioPhoneNumber",
To: request.params.number,
Body: "Start using Parse and Twilio!" + query
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});
I need help making this code work. And advice on books i can read to improve my understanding of JavaScript would be useful.
Was finally able to solve my problem. All i actually needed was Parse.User.current().id. Here is the working code:
var twilio = require("twilio");
twilio.initialize("myAccountSid","myAuthToken");
// Create the Cloud Function
Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
var objectId = Parse.User.current().id ;
twilio.sendSMS({
From: "myTwilioPhoneNumber",
To: request.params.number,
Body: "Your Apps verification code is " + objectId
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});

Error handling in Ext.util.JSONP.request

I am trying to connect to a server and when the server is down, the store should do something. All I need to know is how to catch that error/exception in Sencha touch. Any help would be greatly appreciated.
Ext.util.JSONP.request({
url: "http://"+serverAdd+":"+ port+"/users/searchresults.json",
format: 'json',
callbackKey: 'callback',
params : searchCriteria,
callback: function(data) {
store.getProxy().clear();
store.data.clear();
store.sync();
if(data.length < 10){
store.add({title: 'No Results found.'},
{company: 'Please try again !!'},
{posting_date: new Date()});
}
else{store.add(data);}
},
failure: function ( result) {
alert('Failed');
console.log( 'Server not connected, please try again .. ');
}
});
It's probably too late for you, but someone on the Sencha forums modified the Ext.util.JSONP.request method to include setting the "onerror" attribute of the created tag. The post is here. Unfortunately, this method requires modifying the Sencha library code to implement.

Categories