fs.unlink() does not work and callback does not fire - javascript

It was working a while ago, and I'm not sure what changed to where the fs.unlink() function seems to be totally skipped. The file for deletion is saved in the root package directory, while the typescript file that contains my code is in the dist folder. It worked when I passed in only the fileName like this: fs.unlink(fileName). But now that doesn't work, and it also doesn't it work when I explicitly direct it to the root folder by using either fs.unlink("./" + fileName) or fs.unlink("../" + fileName), or even when I hardcode the path!
Below is my code:
s3.putObject(params, function(err, data){
// ...
console.log("Data uploaded to S3 bucket");
fs.unlink("./" + fileName, function(err){
if(err) {
return console.log("Delete error: " + err);
}
else{
console.log("file deleted successfully");
}
});
console.log("Before process.exit()");
process.exit();
});
When run, this code logs:
Data uploaded to S3 bucket
Before process.exit()
So as you can see, it skips both the if and else statements on fs.unlink(), and doesn't delete the file. It's acting like that block of code doesn't exist at all and I can't figure out why.

You are using async unlink function that's why it is executing next statement. Use
unlinkSync instead.

Related

fs.rename unable to rename directories with contents on Windows 10

So I've seen several questions similar to this issue but I haven't found anything that quite fixes it.
I'm able to rename a directory with node.js as long as the directory is empty. Once I add contents, I get the error: EPERM: operation not permitted. If I delete the contents, I regain the ability to rename. Is there something simple I'm missing here? Nothing crazy in the code... just can't rename a directory if it has contents.
app.post('/rename', function(req, res, next) {
let _target = req.body.target,
_from = req.body.from,
_to = req.body.to;
fs.renameSync(
'public/' + _target + '/' + _from,
'public/' + _target + '/' + _to,
function(err, data) {
if (err) { sLog(err); return }
res.send('File Renamed');
}
);
});
-- EDIT --
The issue is when the directory has sub directories. Files in the directory to rename don't seem to affect the action
The issue is that I was using nodemon instead of node. Apparently, as a listener, it was locking the files... once I ran everything as node, it functioned as expected.
fs.renameSync doesn't work when the folder is not empty, so you should use another way just like the following:
import fsExtra from "fs-extra";
//then copy and delete, disgusting but work
fsExtra.copySync(oldpath, path);
fsExtra.rmdirSync(oldpath, { recursive: true });

Problems Downloading files using Dropbox JavaScript SDK

I need to figure out where my files are downloading when I use the filesDownload(). I don't see an argument for file destination. Here's my code:
require('isomorphic-fetch');
var Dropbox = require('dropbox').Dropbox;
var dbx = new Dropbox({ accessToken: 'accessToken', fetch});
dbx.filesDownload({path: 'filepath}).
then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
I'm getting a successful callback when I run the code but I don't see the file anywhere.
I need to know where my files are downloading to and how to specify the file destination in my function.
Thanks,
Gerald
I've used the function as described in the SDK's documentation (http://dropbox.github.io/dropbox-sdk-js/Dropbox.html#filesDownload__anchor) but I have no idea where my file goes.
Expected Result: Files are downloaded to Dropbox to path that I have designated.
Actual Results: I get a successful callback from Dropbox but I cannot find the files downloaded.
In Node.js, the Dropbox API v2 JavaScript SDK download-style methods return the file data in the fileBinary property of the object they pass to the callback (which is response in your code).
You can find an example of that here:
https://github.com/dropbox/dropbox-sdk-js/blob/master/examples/javascript/node/download.js#L20
So, you should be able to access the data as response.fileBinary. It doesn't automatically save it to the local filesystem for you, but you can then do so if you want.
You need to use fs module to save binary data to file.
dbx.filesDownload({path: YourfilePath})
.then(function(response) {
console.log(response.media_info);
fs.writeFile(response.name, response.fileBinary, 'binary', function (err) {
if (err) { throw err; }
console.log('File: ' + response.name + ' saved.');
});
})
.catch(function(error) {
console.error(error);
});

Can fs.unlink() delete a empty or non empty folder?

I am new to Node.js.
const fs = require('fs');
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
This is some code that I copied from a node.js document file system intro example.
But, I am confused. Can unlink() delete a folder or not?
I have tried but it doesn't work.
So, can unlink() delete a folder or not?
The fs.unlink(path, callback) function is used to delete a file not a folder.
To remove a folder you can use the fs.rmdir(path, callback) function instead.

Node.js - Nginx 502

I'm getting a 502 error in my production environment, but not on my local install of my app, for a certain route. I've narrowed the problem down to the callback of the .aggregate() method that I'm querying the database with.
This is the callback:
function(err,f){
var f = f[0].forms;
if(err) console.log(err);
if (callback) {
res.setHeader('Content-Type', 'text/javascript');
res.send(callback + '(' + JSON.stringify(f) + ')');
} else {
res.end("Callback not set");
}
}
It works for my local install. I'm using this callback to send the object to another source using jsonp. Is there something I'm not doing correctly?
If you're logging an error, probably best to not continue to the next line of code.
if(err) {
console.log(err);
return done(err); //or send the error page, or do something other than continue on.
}
Can you see more contents of the error message?

Files is deleting before its used in node js

I'm new to node js and i'm trying to do the following:
function createPasswordfile(content)
{
fs.writeFile(passwordFileName,content, function(err) {
if(err) {
console.log("Failed on creating the file " + err)
}
});
fs.chmodSync(passwordFileName, '400');
}
function deletePasswordFile()
{
fs.chmodSync(passwordFileName, '777');
fs.unlink(passwordFileName,function (err) {
if (err) throw err;
console.log('successfully deleted');
});
}
and there are three statements which call these functions:
createPasswordfile(password)
someOtherFunction() //which needs the created password file
deletePasswordFile()
The problem I'm facing is when I add the deletePasswordFile() method call, I get error like this:
Failed on creating the file Error: EACCES, open 'password.txt'
successfully deleted
Since its non blocking, I guess the deletePasswordFile function deletes the file before other function make use of it.
If deletePasswordFile is commented out, things are working fine.
How should I prevent this?
writeFile is asynchronous, so it's possible the file is still being written when you try and delete it.
Try changing to writeFileSync.
fs.writeFileSync(passwordFileName, content);

Categories

Resources