Im trying to alert a user that the file size is not allowed and the size of the image.
in my code I'm doing:
alert('Error' + [JSON.stringify(err)]);
The alert message returns:
Error[{"message":"File size not allowed","error":"FILE_SIZE","param":10722753}]
I would like the alert to say:
File size not allowed 10722753
How can I clean up this error message and get the details our of the err object?
var err = {"message":"File size not allowed","error":"FILE_SIZE","param":10722753};
document.write('Error ' + err.message + " " + err.param);
You can directly access the JSON properties
alert('Error ' + err.message + " " + err.param);
You need to use the fields of your JSON object:
alert(err.message + " " + err.param );
May be this will help you.
var err=[{"message":"File size not allowed","error":"FILE_SIZE","param":10722753}];
alert("Error Message: "+err[0].message);
You will get:
Error Message: File size not allowed
Related
When I am posting a picture from my electron app to blob storage, sometimes it works, and other times I get this error on my terminal:
When I was first working on this app, this problem never showed up, until a week ago. It occurred without making any changes to this part of the app. Any idea on what could cause it.
The electron app goes white, and the dev tools are disconnected.
Here is the code:
var azure = require('azure-storage');
var blobSvc = azure.createBlobService('*connection keys inside here*');
function createBlob() {
blobSvc.createContainerIfNotExists('photos', {publicAccessLevel : 'blob'}, function(error, result, response){
if(!error){
console.log(response);
}
});
console.log("creating image for student#: " + stud_id);
blobSvc.createBlockBlobFromStream('photos', stud_id + '.jpg', toStream(imgData), imgData.size, function(error, result, response){
if(!error){
console.log("file upload: \n" + JSON.stringify(result) + " \n" + JSON.stringify(response));
createPerson();
}
else if (error) {
console.log("error: " + JSON.stringify(error));
}
});
}
In your code, you actually call the createBlockBlobFromStream immediately, probably without container having created. This may cause the problem.
So, you would need to put them within the callback of the createContainerIfNotExists function:
blobSvc.createContainerIfNotExists('photos', {publicAccessLevel : 'blob'}, function(error, result, response) {
if(!error) {
console.log(response);
console.log("creating image for student#: " + stud_id);
blobSvc.createBlockBlobFromStream('photos', stud_id + '.jpg', toStream(imgData), imgData.size, function(error, result, response) {
if(!error) {
console.log("file upload: \n" + JSON.stringify(result) + " \n" + JSON.stringify(response));
createPerson();
} else {
console.log("error: " + JSON.stringify(error));
}
});
}
});
I stumbled on a very strange issue recently concerning my Node.JS REST API. It's aim is to convert HTTP requests to SQL requests and the SQL response to HTTP response so I can then transfer everything over SSL/TLS and it's encoded.
The problem is that when the response size is small, it all works fine but when it exceed a certain size (about 255.37 KB), the response body is cutted right in the middle of the JSON. After multiple tests it appears that the issue is related to the HTTP response total size (including the headers) because when some custom headers are removed, more of the body is sent. I wrote a similar code in PHP and the response in JSON from the PHP API is fine so I assumed that the issue was originating from a bug in the Node.JS web server. Also, the Content-Lenght header is fine and has the right value (like the PHP API response does).
I'm using Node.JS v6.11.0 with the last release of Express. All my dependencies are up to date thanks to npm.
Here is the code of the function that handle the GET HTTP requests and proceed in doing SELECT SQL requests, then parse to json string the answer
function SelectData(connection){
return function (req, res) {
let tableName = req.params.table;
let id = req.params.id;
console.log("GET request received with tableName = " + tableName + " and id = " + id);
// Should match this regex
if (!tableName.match(/^[\-\_\s0-9A-Za-z]+$/)){
console.log("Error: Invalid table name provided : " + tableName);
badRequestError(req, res);
return;
}
if (id !== undefined){
if (!id.match(/^[0-9]+$/)){
console.log("Error: Invalid id provided : " + id + " (table name was valid : " + tableName + ")");
badRequestError(req, res);
return;
}
}
// if id is empty, then don't use any condition, else, add SQL condition
idPart = (id == undefined) ? "" : ('WHERE id="' + id + '"');
// TODO: try to replace " + var + " by question marks and bind params, find a way to do it w/ id
connection.query("SELECT * FROM " + tableName + " " + idPart + ";", function(err, result){
res.setHeader('Content-Type', 'application/json');
console.log("Request executed successfully !");
// Works too but I prefer the .send() method
// res.status(200).write(JSON.stringify(result)).end();
res.status(200).send(JSON.stringify(result));
req.connection.destroy();
return;
});
}
}
function badRequestError(req, res){
res.setHeader('Content-Type', 'text/plain');
res.status(400).send("Bad Request");
req.connection.destroy();
}
exports.SelectData = SelectData;
Edit: After researching yesterday I found similar issue resulting of the use of the HTTPS module so I removed it but it still happens.
I have some problem with parse cloud js.
After I save ParseMessage object, I want to check 3 pointer (product, sender and recipient) in other class (MessageTracker). With the base 3 query there is no problem, but when I call insideQuery.notEqualTo("sender", result.get("owner")); I got an error:
102 : pointer field sender needs a pointer value
This "owner" also a pointer to user class, like the others (sender and recipient), but this time parse cloud send an error.
(On console I see, that the object ids are valid values, so I have the right objects)
Parse.Cloud.afterSave("ParseMessage", function(request) {
var message = request.object;
var product = message.get("product");
var sender = message.get("sender");
var recipient = message.get("recipient");
var query = new Parse.Query("Product");
query.get(product.id, {
success: function(result) {
console.error("prod owner: " + result.get("owner").id + " sender: " + sender.id + " reciever: " + recipient.id);
var insideQuery = new Parse.Query("MessageTracker");
insideQuery.equalTo("product", product);
insideQuery.equalTo("sender", sender);
insideQuery.equalTo("recipient", recipient);
insideQuery.notEqualTo("sender", result.get("owner"));
insideQuery.find({
success: function(results) {
console.error("count: " + results.length);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
},
error: function(object, error) {
}
});
});
Any good advice?
UPDATE
I maybe found the problem. Parse do not let us check equalTo and notEqualTo for the same pointer in the query. Is there any other way to do that?
Try building out the pointer:
var ownerPointer = {
__type: 'Pointer',
className: <className>,
objectId: result.get("owner").id
};
insideQuery.notEqualTo("sender", ownerPointer);
Below is my JS code that is supposed to delete a pointer reference to a 'Game' object which is stored in an array under the key 'games' in the 'team' object. Fetching works and all the 'success' logs below get called meaning that the .remove and .save functions get called, but for some reason, this does not do anything and the pointers still exist in the arrays after the execution of this code.
What am I doing wrong?
var queryTeam = request.object.get("awayTeamID");
queryTeam.fetch({
success: function(awayTeam){
console.log("Success in Fetching awayTeam Object. Destroying Now.");
awayTeam.remove(awayTeam.get("games"), request.object);
awayTeam.save();
var queryTeam = request.object.get("homeTeamID");
queryTeam.fetch({
success: function(homeTeam){
console.log("Success in Fetching homeTeam Object. Destroying Now.");
homeTeam.remove(homeTeam.get("games"), request.object);
homeTeam.save();
},
error: function(homeTeam, error){
console.error("Error removing game from homeTeam array! " + error.code + ": " + error.message);
}
});
},
error: function(myObject, error){
console.error("Error removing game from awayTeam array! " + error.code + ": " + error.message);
}
});
Your approach to removing is incorrect, you don't need to fetch the array to remove it, the remove request should be made on the object:
awayTeam.remove("games", request.object);
I have recently set up node.js using Express and I created a simple HTML form using Jade. The form is to insert the data in a PostgreSQL database. The problem is that when I press submit on the form, everything is inserted on the database, but the HTML form is just hanging/lingering, and at some point it stops with No data received, ERR_EMPTY_RESPONSE. Sometimes it also inserts the data twice. I guess this is because the server side does not return a response, but I cannot see how (I am new to node.js).
The form has action="add_device" which is routed to routes/add_device.js. add_device.js looks like this:
var express = require('express');
var router = express.Router();
router.get('/', function(request, response, next) {
res.send('Nothing to see here. Move along.');
});
router.post('/', function(request, response, next) {
var db = require('../public/javascripts/db/insert');
var result = db.insertDevice(request, response);
return result;
});
module.exports = router;
The insertDevice function in my db module looks like this (it is exported with module.exports):
// Insert new QA device. Data arriving as a request from a HTML form.
insertDevice: function (request, response) {
// Input that is verified in the HTML form.
// Convert to proper format for PostgreSQL query.
var name = '\'' + request.body.name + '\'';
var ip_address = '\'' + request.body.ip_address + '\'';
var os = '\'' + request.body.os + '\'';
// Input that needs to be verified. Prepare for PostgreSQL query.
var mac_address;
var os_version;
request.body.mac_address == "" ? mac_address = 'NULL' : mac_address = '\'' + request.body.mac_address + '\'';
request.body.os_version == "" ? os_version = 'NULL' : os_version = '\'' + request.body.os_version + '\'';
var pg = require('pg'); // PostgreSQL module.
var td = require('./table_data') // Database constants.
var client = new pg.Client(request.app.get('postgreConnection'));
client.connect(function(err) {
if (err) {
return console.error('Could not connect to postgres', err);
}
var QUERY = "INSERT INTO " + td.QA_DEVICES.TABLE_NAME + "(" +
td.QA_DEVICES.COLUMN_NAME + ", " +
td.QA_DEVICES.COLUMN_MAC_ADDRESS + ", " +
td.QA_DEVICES.COLUMN_IP_ADDRESS + ", " +
td.QA_DEVICES.COLUMN_OS + ", " +
td.QA_DEVICES.COLUMN_OS_VERSION + ") VALUES(" +
name + ", " +
mac_address + ", " +
ip_address + ", " +
os + ", " +
os_version + ");";
client.query(QUERY, function (err, result) {
if (err) {
return console.error('Error running query: ' + QUERY, err);
}
console.log('Query performed: ' + QUERY);
client.end();
});
});
}
The 'Query performed' is always logged to console and data inserted into the database, but the form is still hanging. My questions are:
Is it the lack of response from the server that makes the form hang?
How can I "send a response back" to the front end?
Is it possible to route the front end to another page after insertion into the database? What is the best practice?
Yes, your request is receiving no response, so it is hanging.
In order to send a response, you can either send a blind acknowledgement right when the request is received (that is not dependent upon the success of the query and may be bad practice), or you can send it in the callback.
client.query(QUERY, function (err, result) {
if (err) {
// response.json({status: 'error'});
response.write('Error');
return console.error('Error running query: ' + QUERY, err);
} else {
// You can send json here too
// response.json({status: 'success'});
response.write('Success');
}
console.log('Query performed: ' + QUERY);
client.end();
});
If you want to go to another page, simply parse the incoming response on the client side and do a redirect. Using json is a good way to carry this out. You can also do a response.redirect(url) on the server side too, instead of sending back data. Have fun