Parse Cloud result object is empty - javascript

I got this problem yesterday, couldn't resolved it myself.
Here is my parse.com cloud code :
Parse.Cloud.define("getWorkerInfo", function(request, response) {
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo(request.params.userObjectId);
userQuery.select("firstName", "lastName", "username");
userQuery.first({
success : function(result){
var currentUser = {
"firstName" : result.firstName,
"lastName" : result.lastName,
"username" : result.username
};
response.success(currentUser);
},
error : function(error){
response.error(error);
}
});
});
and result to chrome dev tools command :
result : {}
this code for using result from first code wrote this question :
function get(userObjectId) {
Parse.Cloud.run("getWorkerInfo", { "userObjectId" : userObjectId }, {
success: function(result){
return result;
}
,
error : function(err){
console.log(err);
}
});
}
I also changed to response data - user query directly.
Response action is okay.
But Couldn't retrieved that itself :
result = ParseUser {_objCount: 2, className: "_User", id: "iNneJil9XW"}
What I missing, and can I find answer this problem?

Thanks Clark, there's a lot going on in this question. Try hard coding the result just to make sure the execution flow/path is correct and the environment is set up correctly. For example,
Parse.Cloud.define("getWorkerInfo", function(request, response) {
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo(request.params.userObjectId);
userQuery.select("firstName", "lastName", "username");
userQuery.first({
success : function(result){
response.success("1234");
},
error : function(error){
response.error(error);
}
});
});

Related

requete ajax findOne

i had a project contains many problems and i want to get one problem with findOne not all the problems in this project can someone correct for me my code.
this requete ajax which display for me all problems of this project
<div id="people"></div>
<script type="text/javascript">
var data;
var id = "<%= id %>";
var problemes = [];
$.ajax({
url : "/projets/"+id+"/ajax",
data : data,
cache : false,
dataType : "json",
error : function(request, error) {
alert("Erreur : responseText: "+request.responseText);
},
success : function(data) {
data = data.problemes;
data.forEach(function(element) {
var obj = {
id: element._id,
parentId:null,
name:element.text
}
problemes.push(obj);
});
console.log('aa0', problemes);
console.log('aa', problemes);
var peopleElement = document.getElementById("people");
var orgChart = new getOrgChart(peopleElement, {
color: 'FF0000',
primaryFields: ["name", "title"],
photoFields: ["image"],
dataSource: problemes
});
}
});
</script>
and this a route which returns a json data
router.get("/:id/ajax", function(req, res){
Projet.findById(req.params.id).populate("problemes").exec(function(err, data){
console.log("data", data);
if(err){
console.log(err);
} else {
res.status(200).json(data);
}
});
});

Sending a variable from JavaScript to Java

I've created a game that collects a User's time on the browser with JavaScript. What I'd like to do is grab that JavaScript data and send it to the backend, which is written in Java (I'm using a local server running on Tomcat). How can I accomplish this?
I've looked into AJAX and here is what I've come up with...
var myTime = // however long it took for user to win game
var data = {}
data["myTime"] = myTime;
$.ajax({
type : "POST",
url : "/path-to/hosting/save",
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
contentType:'application/json',
success : function(data) {
console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});
When I finish the game, I receive this error on the console:
statusText:"parsererror"
My initial thought was that I didn't form my JSON correctly, but I am not sure. Any help would be appreciated. Thanks in advance!
As your code it seems like myTime is a single value why you store it in array you can pass that as follows
var myTime = // however long it took for user to win game
//var data = {}
//data["myTime"] = myTime;
$.ajax({
type : "POST",
url : "/path-to/hosting/save",
data : JSON.stringify({
'myTime': myTime
}),
dataType : 'json',
timeout : 100000,
contentType:'application/json',
success : function(data) {
console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});

Why is my AJAX error not returning my Webmethod exception?

I'm using google chrome for my work.
Following https://stackoverflow.com/a/26003200/7204173 I tried to return an exception from webmethod. But it does not show anything in the console.log. The only thing I got is from network tab the generic error message below :
"An error occurred while updating the entries. See the inner exception for details."
So what I tried is below (ps : I'm sending DML a database using entity framework DbContext) :
public string AddData(myEntity e)
{
using(var context = new MyEntityModel())
{
try
{
context.myEntity.Add(e);
context.SaveChanges();
return "success";
}catch(Exception ex)
{
return "error : " + ex.Message;
}
}
}
Now my Javascript code :
function AddArticle()
{
var e {id : "1", value : "test"};
var DTO = { 'e': e };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(DTO),
url: "MyPage.aspx/AddData",
success: function (response) {
console.log(response);
},
error: function (xhr, status, error) {
var exception = JSON.parse(xhr.responseText);
alert(exception.Message);
console.log(exception.Message);
}
});
}
So let's say I put it a call a the javascript function behind an input button. Since it is a database, the var e {id : "1", value : "test"}; can't be added twice since id is a primary key. Still I get nothing in the console.log ; the alert box does not even get triggered. But when it succeeds, the console.log displays success as expected.
Any idea of where I shoud be looking for to find my error ?
Have you try to log the others errors args ?
console.log(xhr);
console.log(status);
console.log(error);
You should be able to parse manually logs in the console and find the right path to the variable you want.
Update
console.log(error); was the answer in the case of OP

Parse Server Cloud Code Save Object

I am attempting to query an object add 1 to the returned integer and then save this object back to my mLabs database using parse server cloud code.
I can successfully query and add 1 to the object that I would like, however I cannot figure out how to successfully save this back to the database. I have tried many solutions all leading to a Parse Server "request timeout"
Parse.Cloud.define("addRating", function(request, response) {
var currentRatingQuery = new Parse.Query("StudentNotes");
currentRatingQuery.equalTo("objectId", "Y4bBzvsHb1");
currentRatingQuery.select("noteRating");
currentRatingQuery.find({
useMasterKey: true,
success: function(results) {
//var noteRating = results.get("noteRating");
//noteRating += 1;
results = Number(results);
results += 1;
console.log("NOTE RATINGGGGG: " + results);
console.log("TYPE OFFFFFFF: " + typeof results);
results.set('institution', "TEST INSTITUTION");
results.save(null, {
useMasterKey: true
});
console.log("SAVE SUCCESS", results);
response.success("rating updated successfully.", results);
},
error: function(error) {
response.error("failed to add 1 to parse cloud code rating. Error: " + error); //THIS GETS CALLED
}
});
});
The code above successfully queries the database, but does not save the value back. It results in a Parse Server "request timeout".
My problem was syntax related there is a severe lack of syntax for parse server cloud code due to it being so similar to parse.com cloud code. The following is the working code to retrieve an object and save the object back.
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
success: function(Objects) {
Objects.save(null, {
useMasterKey: true,
success: function(object) {
object.increment("noteRating");
object.save();
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}
});
}
});
});
Timeout may cause by no response.
Try to add response when api failed, and see what is happened.
BTW, If you what response when increment success, response when save complete.
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
//this function will get at most one object
success: function(object) {
if(object){
object.increment("noteRating");
object.save(null,{
//do not use master key?
success: function(note){
//u should wait the non-blocking call success and finish
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, error: response.error
});
}else{
response.error('this student note is not exist');
}
}, error: response.error
});
});
If this object is existed, you can just rewrite your code as below
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var studentNote = new SaveObject();
studentNote.id = request.params.objectId;
studentNote.increment("noteRating");
//student.save(null,{useMasterKey:true}).then(
studentNote.save().then(
function(note){
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, response.error
)
});

Error : expecting ] token after element when using ajax.get

I keep getting an error when i run the debugger that says "SyntaxError: missing ] after element list"
the first request always works as intended but all of the follow requests return a syntax error. can someone explain to me what i'm messing up here. please and thankyou.
$("#submitmsg").click(function(){
alert('start');
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
setInterval($.ajax({
url : 'http:www.whatever.com/log.html',
type : 'GET',
success : function(data)
{
document.getElementById('chatframe').innerHTML = data;
}
}), 500);
</script>
your setInterval is wrong, it should be, setInterval(func, delay[, param1, param2, ...]); like
setInterval(ajax_func, 500);
function ajax_func() {
$.ajax({
url : 'http://www.whatever.com/log.html',
type : 'GET',
success : function(data) {
document.getElementById('chatframe').innerHTML = data;
}
});
}

Categories

Resources