I'm uploading a File with Angular to my Node server. Sometimes the image is not uploaded and I get req.files "undefined" in the server side and I can not figure it out why. I think it has not to do with the weight of the image: the image that is uploaded is around 48KB and the one that isn't 16KB. I attach two snapshot to show the difference in timing (http post) between the one that works and the other (network tab of Chrome developer tool). I can see that the "times" ("blocking", "sending") are not overlapping themselves in the one that is failing. Maybe is that the problem... but I'm not sure about how to fix it.
I got it.
I was using express.multipart in the server side with defer option "express.multipart({defer: true})"
Because of that, sometimes the execution reached the code where I manage the post request to get the image with "req.files.file" before form's "end" event in express.multipart module is fired where the module sets req.files:
form.on('end', function(){
if (done) return;
try {
req.body = qs.parse(data);
**req.files = qs.parse(files);**
if (!options.defer) next();
} catch (err) {
form.emit('error', err);
}
});
So sometimes req.files.file = undefined, simply because is not set yet...
Getting rid of {defer: true} option solves my problem. Nothing to do with one or another image.
Here you are the doc of "defers" option in express.multipart module:
"defers processing and exposes the Formidable form object as req.form.
* next() is called without waiting for the form's "end" event.
* This option is useful if you need to bind to the "progress" event, for example."
Related
I am getting the following error when trying to run my server (index.js):
Error: TypeError [ERR_INVALID_URL]: Invalid URL: index.js
The code block is here:
app.get('/:url', async function (req, res) {
try {
return res.status(200).json(data);
} catch (ex) {
console.log(ex);
return res.status(500).json({ message : "Oops." });
}
With the specific line of code it is referring to is:
const site = await .open(decodeURIComponent(req.params.url));
Has anyone ever encountered this error before, and have any idea how to fix it in this context? Not sure how it is throwing an error for my entire index.js server
Note: This is an express app
The value of req.params.url is index.js.
A web browser can expand index.js into an absolute URL because it knows that it can use the URL of the current HTML document as the base URL.
You're running your code in Node.js. It doesn't run inside a document. There is no base URL.
You need to provide an absolute URL (e.g. http://example.com/index.js) yourself.
That said, I suspect wappalyzer may require that you give it the URL to an HTML document not a JS file.
Well, since there isn't much that i can see about your usage, I'm just going to assume you went to something like http://localhost/index.js.
The problem now is, that wappalyzer does not actually get a valid url. It gets index.js without any form of protocol (http/https/ws/wss...). This means it doesn't know what to do and tells you that the url you provided is invalid.
In order to fix this go to http://localhost/https%3A%2F%2Flocalhost%2Findex.js or append https:// or something similar to your parameter.
So basically I wrote a simple API where you can perform http POST and http GET on a nedb.
My problem however is, that after posting an entry (customer), the entry appears in the nedb file but when I use the GET method it isn't returned.
However after restarting the API it works as expected and the expected data is returned. But why does it have to restart in order to "refresh" the data?
Get-Function:
const db = new Datastore({filename: __dirname + '/db.dat', autoload:true});
export function getAll(req: Request, res:Response, next: Next):void{
db.find({}, (err, customers) => {
res.send(customers);
});
next();
}
In situations where you get weird errors or something strange is not working with nedb and you cannot figure out what's wrong, try to delete the .dat file and try again.
Often you accidentally modify the nedb file (e.g. with autosave of your editor) and you corrupt it.
I'm gonna start by showing you my code
Angular Code: app.js
$scope.submit = function(){
$scope.jsonData={
"status":"OK",
"data":{
"nbOperatorPresent": $scope.nbOperatorPresent,
"objectif1stHour": $scope.objectif1stHour,
"objectif2ndHour": $scope.objectif2ndHour,
"objectif3rdHour": $scope.objectif3rdHour,
"objectif4thHour": $scope.objectif4thHour,
"objectif5thHour": $scope.objectif5thHour,
"objectif6thHour": $scope.objectif6thHour,
"objectif7thHour": $scope.objectif7thHour,
"objectif8thHour": $scope.objectif8thHour
}
}
$http.post("http://localhost:5000/settings",
JSON.stringify($scope.jsonData)).success(function(data,status){
console.log('success')
})
}
NodeJs Code: index.js
app.post('/settings',urlencodedParser,function(req,res){
console.log(req.body)
})
As you can see, I have a button to submit the data inserted by the user and send it to the server.
My problem is, when I hit submit, there is nnothing in my browser console, I mean console.log('Success !') didn't work, that is to say that all the code inner the .success(function(data,status)) won't be executed so I can't notify the user that he has submitted Successfully, I don't know where the problem came from !!
BUT In the other console console.log(req.body) I found all the data that has been passed from Angular.
Can anyone explain this to me ? I've tried other solutions but always the same problem :(
To expand on the answer...
Please note if you are unfamiliar with node and express you may want to get in the habit of returning
res.send(success: true, data: res).end()
Its very typical on the angular or UI side to be able to parse as response.data object. Just a suggestion.
and change to this:
.success(function(res,status){
console.log(res.success, res.data)
})
This is a very common architecture especially when dealing with web services that you have not control over.
You're not returning anything from the node.js code. You need to add in a returning data like:
app.post('/settings',urlencodedParser,function(req,res) {
console.log(req.body)
res.send("Success")
})
Best method for clearing broken images from a database. I have 30,000+ image entries in my mongodb database however many have become broken over time. I want to only return images that aren't broken on the server side. What would be the best method to find all the broken images and add a parameter or delete the broken images from the database?
Currently I am returning all the images to the frontend and doing and hiding on the 'onerror' attribute. Should I export and iterate over all the images somehow and get an export of the image ids? Are there better options or what can I use to do this? Just looking for advice.
You could use Blaze template event handlers to catch image load errors, then flag the image as failed, or just delete it. Self cleaning and always running...
Template.ImageLoader.events({
//TODO: possibly add an extra class name on the img tag, to make sure you only catch errors from your collection.
'error img': function(event, template) {
const url = event.currentTarget.src;
//TODO: find the image in your collection, using the url
//TODO: perform cleanup (flag or delete)
alert(`error loading image ${url}`);
}
});
I would suggest adding an error counter to your collection and $inc it each time a loading error occurs. Once the load error count reaches a certain number, delete it.
I'm having this issue where I register for Push using $ionicPush.register() But I'm not doing user identification at any point using $ionicUser(like the docs suggest). So, I'm only calling $ionicPush.register() and I wait for that promise to resolve and I'm storing the device_token on my server, eg.
$ionicPush.register({
onNotification: function(notification) {
//....
}
}).then(function(deviceToken) {
//Save device-token to my server
UserService.saveIosDeviceToken(user.id, deviceToken)
.then(function(user) {
return user;
})
.catch(function(error) {
console.log(error);
});
});
I have noticed that regardless of calling $ionicUser.identify(...) or not, an $ionicUser will be created (which you can see in the Dashboard). Now the issue that I'm having is that I'm not always getting a device token. ie. I end up with some(not all) Ionic Users with no device tokens (therefore no device token I can store on my server), eg.
Do you guys know what's going on? I'm reading the FAQ here and it says: > "If you are using $ionicPush.register() without passing a user object, you should make sure you are waiting for the $ionicPush.identify() promise to complete." -- Could this be the likely cause? and How do I pass a user to $ionicPush.register()?
Let me know what you guys think, I really need to know what I'm doing wrong.
Regards,
-J