Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to load resource: the server responded with a status of 400 (Bad Request) #4788

Closed
byghuutran-zz opened this issue May 26, 2018 · 5 comments

Comments

@byghuutran-zz
Copy link

byghuutran-zz commented May 26, 2018

system logs:
`*** Invalid function: "weekChanged" code=141, message=Invalid function: "weekChanged"

*** Error handling request: ParseError { code: 141, message: 'Invalid function: "weekChanged"' } code=141, message=Invalid function: "weekChanged"

Parse.Cloud.define("weekChanged", function(request, response) {
	//add record for the user to NotificationCount Class
	var currentUserId = request.params.currentUserId;									   
	var notificationLeftWeek;	
	var userCount = 0;
   	var NotificationCount = Parse.Object.extend("NotificationCount");
	console.log("NotificationCount Query Find Starting");
	var query = new Parse.Query(NotificationCount);
	query.equalTo("currentUserId", currentUserId);
	
	query.count({
		success: function(count) {
			//alert("Successfully retrieved " + results.length + " scores.");
			// Do something with the returned Parse.Object values

			console.log("NotificationCount Query Count Successful " + count);
			userCount = count;
			
			if (userCount == 0){
				console.log("Inside notificationCount Record Add. Count: " + userCount);
		
				var NotificationCount = Parse.Object.extend("NotificationCount");
				var notificationCount = new NotificationCount();
									
				notificationCount.set("currentUserId", currentUserId);
				notificationCount.set("date", new Date() + "");					
				notificationCount.set("count", notificationAllowedPerWeek);
				notificationCount.set("weekCount",0);
				notificationCount.save(null,{
					success: function(notificationCount){
						response.success(notificationAllowedPerWeek);						   
					},
					error: function(notificationCount, error){
						response.error(error.code + " " + error.message);
					}		   
				});		
			}else{
				var NotificationCount = Parse.Object.extend("NotificationCount");
				console.log("NotificationCount Query Find Starting");
				var query = new Parse.Query(NotificationCount);
				query.equalTo("currentUserId", currentUserId);
				query.find({
					success: function(results){
						for (var i = 0; i < results.length; i++) { 
							var objectNotificationCount = results[i];
							notificationLeftWeek = objectNotificationCount.get('count');
							creationDate = objectNotificationCount.get('date');
							//alert("Date"+object.get('date'));
								  
							weekCount = objectNotificationCount.get('weekCount');
							//$('#notificationCountServer').html('<strong>'+notificationLeft+'</strong>');
							//alert(object.id + ' - ' + object.get('count'));
							//alert("Date 2" +creationDate );
							
							var date1 = new Date(creationDate);
							var date2 = new Date();
							
							//alert(date2);
							var one_day=1000*60*60*24;
									
							// Convert both dates to milliseconds
							var date1_ms = date1.getTime();
							var date2_ms = date2.getTime();
							
							// Calculate the difference in milliseconds
							var difference_ms = date2_ms - date1_ms;
								
							// Convert back to days and return
							var diff = parseInt(Math.round(difference_ms/one_day)/7);							
							
							//alert("diff "+diff);
							console.log("Creation time: " + date1);
							console.log("Current time: " + date2);
							console.log("difference min: " + diff);
							console.log("weekCount: " + weekCount);							
							console.log("notificationLeftForWeek: " + notificationLeftWeek);

							if(diff > weekCount && notificationLeftWeek <= notificationAllowedPerWeek){								
								notificationLeftWeek = notificationAllowedPerWeek;
								//	$('#notificationCountServer').html('<strong>'+notificationLeft+'</strong>');
								objectNotificationCount.set("count", notificationLeftWeek);
								objectNotificationCount.set("weekCount",diff);
								objectNotificationCount.save();
								response.success(notificationAllowedPerWeek);									
							}else{
								response.success(notificationLeftWeek);
							}
						}
					},
					error: function(){
						response.error(error.code + " " + error.message);
					}
				});								
			}
		},
		error: function() {			
			//alert("Error: " + error.code + " " + error.message);
			userCount =0;
		}
	});
});

Parse.Cloud.define("notification", function(request, response) {
	var triggerValue = request.params.triggerValue;
  	var settingValue1 = request.params.settingKey1;
  	var settingValue2 = request.params.settingKey2;
  	var settingValue3 = request.params.settingKey3;
  	var settingValue3 = request.params.settingKey3;
   	var currentUserId = request.params.currentUserId;
   	var notificationLeft;
	var objectNotificationCount;
   
   	/*Parse.Cloud.run('checkNotificationCount', {currentUserId: currentUserId}, {
		success: function(success) {
			console.log("notificationLeft "+notificationLeft);
			notificationLeft = success;
		},
		error: function(error) {
			alert("Error Find Trigger: " + error + " " + error.message);
		}
	});*/
   
   	console.log("Send Notification Begin");
    var NotificationInitialCount = Parse.Object.extend("NotificationCount");
	var queryInitial = new Parse.Query(NotificationInitialCount);
	queryInitial.equalTo("currentUserId", currentUserId);
	console.log("Send Notification UserId: " + currentUserId);
	try{		
		queryInitial.find({
			success: function(results) {
				//alert("Successfully retrieved " + results.length + " scores.");
				// Do something with the returned Parse.Object values
				console.log("success");
				// response.success(results[0].get('count')); 
				for (var i = 0; i < results.length; i++) { 
					objectNotificationCount = results[i];
					notificationLeft = objectNotificationCount.get('count');
				}
				if(notificationLeft > 0){
		  			var query = new Parse.Query(Parse.Installation);
		  			query.equalTo("installationId", request.params.installationId);											
		  			Parse.Push.send({where: query, // Set our Installation query
							data: {					  
								triggerKey:triggerValue, 
								settingKey1:settingValue1,
								settingKey2:settingValue2,
								settingKey3:settingValue3,
								objectType:"android",
								action:"com.huu.library.findmymobile.MESSAGE"
							}
						},{success: function() {
							// Push was successful
							objectNotificationCount.set("count", notificationLeft-1);
							objectNotificationCount.save();
							response.success(notificationLeft-1);								
						},
						error: function(error) {
							// Handle error
							response.error("Error " + error.code + " " + error.message);
						}
					});
				}else{
					response.error("Only " +  notificationAllowedPerWeek + " notifications per week allowed");
				}
			},
			error: function(error) {
				console.log("Error: " + error.code + " " + error.message);
				response.error("Error: " + error.code + " " + error.message);
				alert("Error: " + error.code + " " + error.message);
			}
		});
	}catch(e){
		console.log(e);
	}
   	console.log("notificationLeft "+notificationLeft);
});

@flovilmart
Copy link
Contributor

What is not working? Can you provide server logs when running with VERBOSE=1? Also consider stackoverflow if your question is about implementation details.

@byghuutran-zz
Copy link
Author

i have problem with weekChanged Function and Notification Function, can you pls give me solution with this implementation details?

@flovilmart
Copy link
Contributor

I’m sorry but we use GitHub issues for reporting bugs with parse server, not debugging or support of your code. Please open an issue on stackoverflow, you may have a better support there

@byghuutran-zz
Copy link
Author

Can you please spend a little time take a look on my code, i have open an issue on stackoverflow but don't receive a answer. i think the code is true and have a small error, I am using cloud code functions heavily in my app and i have some confused!
:( :(

@flovilmart
Copy link
Contributor

Again, we don’t do support for user code. Sorry I can’t be of any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants