I'm doing the initialize as i'm supposed to, with the right keys etc.
Even when doing so right within the function that is later using Parse.Cloud.run, I still get 401 unauthorized.
Is there a way to see if the initialize worked properly?
does it return any kind of response? error? if so, how do I go about seeing said response?
the initialize is currently simply:
Parse.initialize("appid", "javascript key"); (with the correct keys of course).
then I call a cloud function:
Parse.Cloud.run('testfunction', aUserObj, {
success: function(result) {
supersonic.ui.dialog.alert(result);
},
error: function(error) {
supersonic.ui.dialog.alert(error);
}
});
and the error is 401 unauthorized (which I also see in the javascript console as POST api.parse.com..... 401 unauthorized).
btw - using curl and rest api key I can make it work without a problem, so it's not an actual permissions issue as best I can tell.
Thanks.
You are right #nopro
The problem is the parameter format which makes the whole request omit app and javascript key which results in 401 error.
Thank you
I was trying to call function like this:
Parse.Cloud.run('sendMessage', message, {
success: function(success) {
$scope.isLoading = false;
},
error: function(error) {
$scope.isLoading = false;
}
});
but I should have done it like this:
Parse.Cloud.run('sendMessage', {"message":message.id}, {
success: function(success) {
$scope.isLoading = false;
},
error: function(error) {
$scope.isLoading = false;
}
});
Related
I have the following AJAX that will send the entered data to the node server and the controller will check whether such data exist in the database or not.
If I do enter the data correctly, then everything is working fine.
However, I tried enter anything that the database does not have and it immediately throw an error, causing the server to stop. The error said that I did not handle the event, so I tried with res.json(err) in the controller instead of throw new Error, hoping that the error will be passed back to AJAX under the error key, but it is still not working. The error still gets thrown and the node server terminate itself.
I would like the server to continue and alert to the user that the data that was entered is not in the database but I have no idea why my approach is not correct.
I was thinking of using this SO thread if I'm able to get the error message back first from server side.
jQuery Ajax error handling, show custom exception messages
To solve the server from stopping, I used the code in app.js that was referred from this link
How do I prevent node.js from crashing? try-catch doesn't work
I'm not sure whether should I use the accepted answer for my case.
function createProduct(inputval){
let inputAction = window.location.pathname;
$.ajax({
type: "POST",
url: inputAction,
data: {order: inputval.split('-')[0].trim(), lot: inputval.split('-')[1].substring(0,5)},
success: function(data) {
$('#product').val('');
//Another function to add HTML
display(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("XHR" + jqXHR)
console.log("Status" + textStatus)
console.log(errorThrown)
}
});
}
Controller File
exports.createProduct = function (req, res) {
db.Product.findOne({ "order": req.body.order, "lot": req.body.lot }).exec(function (err, product) {
if (!product || err){
throw new Error("The product entered returns null");
}
res.json(product);
});
};
Main File: app.js
process.on('uncaughtException', function (err) {
console.error(err);
console.log("Node NOT Exiting...");
});
You should use correct status code for your response. I suggest change your controller like below snippet
exports.createProduct = function (req, res) {
db.Product.findOne({ "order": req.body.order, "lot": req.body.lot }).exec(function (err, product) {
if (err){
res.status(500).end();//means internal server error
} else if (!product) {
res.status(404).end();//means product not found
} else {
res.json(product);
}
});
};
I finally figure it out thanks to feedback from other community, so I thought I would just share it here. It's so simple and silly me for neglecting such statement.
First, the code in app.js can just be removed.
Second, based on the answer given by #Milad Aghamohammadi. Instead of just:
res.status(500).end();
Use:
return res.status(500).json({err: "Server error"});
This way, the error is able to be handled by the AJAX error function and the node server will not be terminated from the event loop.
So I'm new in angular and I'm new with API's
I tried to create thingy, where I can get video information by id, using several tutorials and browsing stackoverflow
I managed to find 2 solutions to the problem, but neither of them work for me.
First I tried
mvcApp.service('ApiCall', ['$http', function($http) {
var result;
this.GetApiCall = function() {
result = $http.get('https://hosting.com/webmasters/video_by_id?id=123456789')
.success(function(data) {
result = (data);
})
.error(function() {
alert('Something wrong');
});
return result;
}}]);
It uses good API link, but return No 'Access-Control-Allow-Origin' header is present on the requested resource error.
Next solution I found was:
mvcApp.factory('json', function($http) {
return {
getInformation: function (name) {
var url = 'https://hosting.com/webmasters/video_by_id';
return $http.jsonp(url, {
params: {
id: name
}
});
}
}});
This one returns errors, as it does not recognises var as a link, and return video_by_id?id=123456789:1 Uncaught SyntaxError: Unexpected token : error. But as I tinkered with it and looked at some other examples, found out that adding extension to links, fixes this, but I do not have or know an extension. So any help would be valuable
If your API is not in the same server as your Angular code, you could handle CORS on the requested resource, to avoid the error.
I am having issues trying to gracefully handle $http errors. I am looping over a list of servers to make API calls to for status. The calls that complete successfully for perfectly. The ones that fail are not giving me access to the error information. It is always undefined. Here is the code snippet:
angular.forEach($scope.servers, function (server) {
// blank out results first
server.statusResults = {};
$http.jsonp(server.url + '/api/system/status?callback=JSON_CALLBACK', {headers: { 'APP-API-Key': server.apiKey }}).
success(function (data, status, headers, config) {
server.statusResults = data;
}).
error(function (data, status, headers, config) {
// data is always undefined here when there is an error
console.error('Error fetching feed:', data);
});
}
);
The console output shows the correct 401 error (which I didn't output) and my console error message (which I did output) with an undefined data object.
GET https://server_address/api/system/status?callback=angular.callbacks._1 401 (Unauthorized) angular.min.js:104
Error fetching feed: undefined
What I am trying to do is NOT have Angular display the 401 in the log, and instead I will display it in a graceful way. However since data is undefined I have no way of accessing the information.
I am new to AngularJS, but my example closely matches other examples I've found in the documentation.
I've also tried using $resource instead of the $http and got the exact same problem.
var statusResource = $resource(server.url + '/api/system/status', {alt: 'json', callback: 'JSON_CALLBACK'},
{ status: {method: 'JSONP'}, isArray: false, headers: { 'APP-API-Key': server.apiKey } });
// make status API call
statusResource.status({}, function (data) {
server.statusResults = data;
}, function (err) {
// data is always undefined here when there is an error
console.log(err);
});
I'm probably doing something obviously wrong, but I'm not sure what else to try.
Per the $http docs, body is
The response body transformed with the transform functions.
With the 401 (Unauthorized) error you are getting back, it is quite possible there is no body being returned, hence why it is undefined.
If you want to log the error code, log the status parameter instead. It contains the HTTP Status Code, which should be uniform, unlike response bodies.
Im trying to get a working http.get client function working in Meteor.
However I keep getting my own page as result.
Here is my code:
Meteor.http.get("api.openweathermap.org/data/2.5/weather?q=London,uk", function (error, result) {
if(error) {
console.log('http get FAILED!');
} else {
console.log('http get SUCCES');
if (result.statusCode === 200) {
console.log('Status code = 200!');
console.log(result.content);
}
}
});
I would expect that it returned a json object containing weather information.
Do I miss something here?
Thanks.
Please update the url by adding http:// at beginning.
Moreover make this call from your server, i.e. Make a method that contains the above code and call that method via Meteor.call();
Please see Meteor.methods() and Meteor.call()
I am new to AngularJS. I have a .net MVC WebAPI Restful app running on a IIS server. When I query the api with http://xxx.xxx.xxx.xxx/api/project I get:
[{"Id":1,"Name":"Glenn Block","Created":"0001-01-01T00:00:00"},{"Id":2,"Name":"Dan Roth","Created":"0001-01-01T00:00:00"}]
I created a ProjectCtrl (in a separate empty project) that looks like this:
angular.module('Project', ['ngResource']);
function ProjectCtrl($scope, $resource) {
$scope.project = $resource('http://192.168.1.221/api/project'
);
$scope.project.get(function (data) {
console.log('success, got data: ', data);
}, function (err) {
alert('request failed');
});
}
I always get a failure. I addressed CORS issues on the server and the request header contains
Access-Control-Request-He... x-requested-with
Access-Control-Request-Me... GET
What I find odd is that when I look in firebug it does NOT do a get but rather shows Option project with a status of 200
I am not sure what I missing.
The error mentioned in your comment, "Error: a.push is not a function", is because your response is an array. With $resource, use the query function when your response is an array.
I think the first parameter of the resource call should be resource-based params. If you have none, pass an empty object {} as the first parameter:
$scope.project.get({},function (data) {
console.log('success, got data: ', data);
}, function (err) {
alert('request failed');
});
My guess is that your err object is actually the successfully returned data. If I'm wrong, tell me and I'll delete!