converting Parse.com object to string - javascript

What I have is when a user enters a name into a textbox and clicks a button, the value in the textbox is saved to the Parse database.
What iI'm trying to do is get the name that was added and add it to a div.
On this Live Example, it's basically what I want, except it alerts [object Object] and not thomas in this case.

You need to use the specific part of the result you need. In your case, replace
query.first({
success: function (results) {
alert("Successfully retrieved " + results + " ");
divTag.innerHTML = results.toString();
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
with
query.first({
success: function (results) {
alert("Successfully retrieved " + results.attributes.FirstName + " ");
divTag.innerHTML = results.attributes.FirstName.toString();
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});

Use this:
alert("Successfully retrieved " + results.get("FirstName") + " ");

Related

Parse cloud code is not logging/alerting on success or failure

I am a newbie in Parse.com cloud code. I am trying to query a table which has a particular column having a request parameter which I am supplying. The query is:
Parse.Cloud.define("newPostNotification", function(request, response) {
Parse.Cloud.useMasterKey();
var userId = request.params.userid;
console.log("User Id "+userId);
var query = new Parse.Query(Parse.ViewCount);
query.equalTo('userId', userId);
query.first({
success: function(object) {
/* var userString = request.params.username;
response.success(userString); */
alert("Success!");
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
I am utterly confused as why I cannot see the the alert messages for success or failure! Please rectify me where I am wrong!
You need to pass a JSON string.
For example: console.log({"Log message":"My message!"});
You can use console.log, console.error, or console.warn. Check the guide.
query.first({
success: function(object) {
console.log("Success!");
response.success();
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
response.error("Error " + error.code + " : " + error.message + " ;
}
});

How to add dynamic a javascript variable in jquery?

I add variable length list in a view with jquery.
$("#addItemday").click(function() {
$.get("/Course/AddDayNewRow", function(data) {
$("#DayEditorRows").append(data);
}).fail(function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});
for every partialview, set a index value.for example
<input name="Days.index" autocomplete="off" value="96633b1d-9c0c-4760-9ca8-474ac28bd52a" type="hidden">
I want to add a script for every partialview.
var objCal1 = new AMIB.persianCalendar("objCal1", "dateid");
After append PartialView, i want to get last item added.
$("input[id*='Date']").last(function () {
var ??? = new AMIB.persianCalendar(???, $(this).attr('id'));});
How do i get last item addes, and set name for this variable?
Two questions, two answer:
1) To get the id of the last item that you added:
var last_id = $("input").last().attr("id");
Remember that you have to wait for your AJAX call to return before firing that, so add it within your AJAX function.
2) Name the variable whatever you like.
Here's an example of the total code:
$("#addItemday").click(function() {
$.get("/Course/AddDayNewRow", function(data) {
$("#DayEditorRows").append(data);
var last_id = $("input").last().attr("id");
var amib_var = new AMIB.persianCalendar(last_id);
// DO SOMETHING WITH THE AMIB_VAR
}).fail(function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
});
});

Display loading image in get method with knockout bindings

Simple function to get and load data from server:
function getdata(stepNumber){
return $.get("./api/data_count.php", {stepNumber: stepNumber})
.fail(function (textStatus, errorThrown){
console.error("Error ! Unable to get step " + number + " count." + "Error: " + errorThrown + ", Status: " + textStatus);
});
}
Using .done differed method to assign data to knockout observable:
getdata(1).done(function(data){
self.dataCount($.parseJSON(data));
});
through following html:
<td><span data-bind = "text: dataCount"></span></td>
All is working well with the code except that it takes around 15 seconds for query to return this count and I am not sure how to display a loading image or message with in following span while the response is awaited.
<span data-bind = "text: dataCount"></span>
It should be just a matter of toggling an observable to show and hide the loader.
something like:
var Vm = function() {
var self = this;
self.showLoader = ko.observable(false);
self.showResults = ko.pureComputed(function() {
return !self.showLoader();
})
self.getdata = function(stepNumber) {
self.showLoader(true);
return $.get("./api/data_count.php", {
stepNumber: stepNumber
})
.done(function(data) {
self.dataCount($.parseJSON(data));
self.showLoader(false);
})
.fail(function(textStatus, errorThrown) {
console.error("Error ! Unable to get step " + number + " count." + "Error: " + errorThrown + ", Status: " + textStatus);
self.showLoader(false);
});
}
return self;
}
<table>
<tr>
<td><span data-bind="text: dataCount, visible: showResults"><img src='path/to/loader' data-bind="visible: showLoader" /></span>
</td>
</tr>
</table>

JavaScript .load does not show correct page results after .click

The below code allows the user to click as button and reject a friend request. Even though multiple results are shown on a page, it functions correctly as it uses wrapper.children('.decline').click(function() { to target the correct result.
After this happens the following $( "#containerFriends" ).load("friends.html #containerFriends" ); should refresh the page so that the latest results are displayed. However a completely blank page is shown, even when results should still exist. If I manually refresh the page, the correct results are shown.
I'm not sure what is wrong with the below and what is causing such an issue?
mainQuery.find({
success: function(results) {
var friends = [];
for (var i = 0; i < results.length; i++) {
friends.push({
imageURL: results[i].get('toUser').get('pic'),
username: results[i].get('toUser').get('username'),
userId: results[i].get('toUser').id,
status: results[i].get('status'),
// Saves the object so that it can be used below to change the status//
fetchedObject: results[i]
});
}
var select = document.getElementById("FriendsConnected");
$.each(friends, function(i, v) {
var opt = v.username;
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
})
$('#containerFriends').empty();
$('#containerFriendsConnected').empty();
_.each(friends, function(item) {
var wrapper = $('<div class="portfolio-item-thumb one-third"></div>');
wrapper.append('<img class="responsive-image friendImgOutline" src="' + item.imageURL + '" />' + '<br>');
wrapper.append('<div class="tag">' + item.username + '</div>');
wrapper.append('<div type="button" class="btn btn-danger mrs decline">' + 'Unfriend' + '</div>');
$('#containerFriends').append(wrapper);
//The following lets the user accept or decline a friend request by changing the status the status from Pending to Declined/////
wrapper.children('.decline').click(function() {
$(".decline").click(function() {
item.fetchedObject.set("status", "Rejected");
$( "#containerFriends" ).load("friends.html #containerFriends" );
item.fetchedObject.save(null, {
success: function(results) {
console.log("REJECTED");
},
error: function(contact, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
alert("Error: " + error.code + " " + error.message);
}
});
});
});
});
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
I actually resolved this by calling the function again, this then ensured the data was upto date and refreshed the page. I believe the issue was that $('#containerFriends').append(wrapper); did'nt contain the updated data after the user .click

Ajax post not sending data out

var dataString = 'edulevel='+ edulevel
+ '&course=' + course
+ '&financerelated=' + financerelated
+ '&occupation=' + occupation
+ '&joblevel=' + joblevel
+ '&income=' + income
+ '&bankname=' + bankname
+ '&acctype=' + acctype
+ '&accno=' + accno;
//ajax
$.ajax({
type:"POST",
url: "process/veriamateur.php",
data: dataString,
success: success(),
error:function(jqXHR, textStatus, errorThrown){
alert("Error type" + textStatus + "occured, with value " + errorThrown);
}
});
I have checked and made sure that dataString was sending out correct stuff, however, the ajax was just not sending out any data, no error whatsoever. Even when I changed the url to an invalid one it still went to my success function.
You should pass data as an object instead of a string when you are sending via POST
Example:
data = {
'edulevel': edulevel,
'course': course
(.....)
};
I have made some changes and now this is working your callback function was success() and jQuery was trying to find the function, either you can write your function at same place or you can write a stand alone function and assign it to sucess:, if you are still getting problem try to change your url, if your current files location is /files/file.php
then your veriamateur.php must be /files/process/veriamateur.php
var dataString = 'edulevel='+ edulevel
+ '&course=' + course
+ '&financerelated=' + financerelated
+ '&occupation=' + occupation
+ '&joblevel=' + joblevel
+ '&income=' + income
+ '&bankname=' + bankname
+ '&acctype=' + acctype
+ '&accno=' + accno;
//ajax
$.ajax({
type:"POST",
url: "process/veriamateur.php",
data: dataString,
success: function(){ alert('success');},
error:function(jqXHR, textStatus, errorThrown){
alert("Error type" + textStatus + "occured, with value " + errorThrown);
}
});

Categories

Resources