Error: ENOENT, open 'null/60c0f337f3413edbc5eb3bb27fa3269f' - javascript

var conn = mongoose.createConnection(config.db);
conn.once('open', function (err) {
if (err) {
next(err);
return;
}
var source = fs.createReadStream(req.files.sample.path);
var gfs = Grid(conn.db, mongoose.mongo);
var id = mongoose.Types.ObjectId();
var target = gfs.createWriteStream({
_id: id,
filename: req.files.sample.name
});
source.pipe(target).on('close', function () {
project.documentation = id;
persistProject(req, res, next, project);
});
}
When I'm trying to upload it os showing the below error.
Terminating application: undefined
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open 'null/60c0f337f3413edbc5eb3bb27fa3269f'
Why this error is coming? Please help me to solve it.

ENOENT: Error, No Entry.
Or to make it more precise: req.files.sample.path seems not to be the correct path.
The error says 'null/...' - so maybe req.files.sample.path is empty?
If I would dig further, I would ask myself: Where do you "upload" to and what are the default paths there? Is that mongoDB working correctly on command line?

Related

ssh2 nodejs | upload file to sftp | Error: Permission denied at 101

When trying to upload a file to sftp server permission denied error appears. The same operation works if file is transferred via FilezIlla.
const UploadFiletoFTP = () => {
let Client = require('ssh2').Client;
var connSettings = {
host: 'abc.com',
port: 22,
username: 'user',
password: 'pass',
};
var conn = new Client();
conn
.on('ready', function () {
conn.sftp(function (err, sftp) {
try {
if (err) {
console.log(err);
throw 'error ' + err;
}
console.log('connected');
var fs = require('fs'); // Use node filesystem
var readStream = fs.createReadStream(
require('path').join(
__dirname +
'/audio/test_data_25_05_2022_09_58_00.zip'
)
);
sftp.readdir(
'speech/non-english',
function (err, list) {
if (err) throw err;
// List the directory in the console
console.dir(list);
// Do not forget to close the connection, otherwise you'll get troubles
conn.end();
}
);
var writeStream = sftp.createWriteStream('SpeechIQ', {
flags: 'a', // w - write and a - append
encoding: null, // use null for binary files
mode: 0o666, // mode to use for created file (rwx)
});
writeStream.on('close', function () {
console.log('- file transferred succesfully');
});
writeStream.on('end', function () {
console.log('sftp connection closed');
conn.end();
});
readStream.pipe(writeStream);
} catch (err) {
console.error(err);
}
});
})
.connect(connSettings);
};
UploadFiletoFTP();
When the above code is run below error appears:
events.js:377
throw er; // Unhandled 'error' event
^
Error: Permission denied
at 101
Emitted 'error' event on Client instance at:
.
.
.
.
code: 3
}
Please advise if I am missing something.
Below snippet lists the files in the directory but the writestream is not working.
sftp.readdir(
'speech/non-english',
function (err, list) {
if (err) throw err;
// List the directory in the console
console.dir(list);
// Do not forget to close the connection, otherwise you'll get troubles
conn.end();
}
);
I had to give the filename to which the data has to be uploaded
var writeStream = sftp.createWriteStream('SpeechIQ/filename.zip', {
flags: 'a', // w - write and a - append
encoding: null, // use null for binary files
mode: 0o666, // mode to use for created file (rwx)
});

GCS catch error if file not found in bucket while streaming download

I am trying to fetch images from google cloud storage bucket from browser and serving the files using API in express. Following code breaks when the Image path is invalid. The try catch doesn't catch the file not found error. Am I missing something here? This code works if an image file exists.
From the Express side, I am using wildcard route (i.e. /*) as the API can accept any image path coming in and try to serve it.
const express = require('express')
const {Storage} = require('#google-cloud/storage');
let server = express()
const storage = new Storage();
const bucketName = '<some-bucket-name>'
server.get('/*', async (req, res) => {
const widthString = req.query.width
const heightString = req.query.height
const format = req.query.format
const fileName = req.path.substring(1);
console.log("url: ", req.path)
let width, height
if (widthString) {
width = parseInt(widthString)
}
if (heightString) {
height = parseInt(heightString)
}
res.type(`image/${format || 'png'}`)
try{
await storage.bucket(bucketName).file(fileName).createReadStream().pipe(res)
} catch (err) {
console.error(err);
}
})
Error:
url: /media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
events.js:377
throw er; // Unhandled 'error' event
^
ApiError: No such object: assets/media/artist/banner_image/16118/screenshot_2019_12_16_at_10.35.24_am.png
at new ApiError (/home/ubuntu/imageoptimizer/node_modules/#google-cloud/common/build/src/util.js:73:15)
at Util.parseHttpRespMessage (/home/ubuntu/imageoptimizer/node_modules/#google-cloud/common/build/src/util.js:175:41)
at Util.handleResp (/home/ubuntu/imageoptimizer/node_modules/#google-cloud/common/build/src/util.js:149:76)
at Duplexify.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/#google-cloud/storage/build/src/file.js:888:31)
at Duplexify.emit (events.js:400:28)
at PassThrough.emit (events.js:400:28)
at onResponse (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:222:19)
at PassThrough.<anonymous> (/home/ubuntu/imageoptimizer/node_modules/retry-request/index.js:163:11)
at PassThrough.emit (events.js:412:35)
at /home/ubuntu/imageoptimizer/node_modules/teeny-request/build/src/index.js:191:27
Emitted 'error' event on PassThrough instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
code: 404,
Posting my previous comment as an answer for visibility
You have to handle this exception by yourself. GCP won't throw the error directly. It only returns 404 as an output, and you have to handle it manually rather than expecting try{} catch () {} to catch this exception. Or you can request it as a new feature in issue tracker, however I am not sure how long it will take for the Google to implement this feature.

AssertionError [ERR_ASSERTION]: handler (func) is required in mongodb

I am using mongooose to connect mongodb but i am getting following error
/Users/uchitkumar/api/node_modules/mongodb/lib/mongo_client.js:804
throw err;
^
AssertionError [ERR_ASSERTION]: handler (func) is required
at new AssertionError (internal/errors.js:315:11)
at _toss (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:22:11)
at Function.out.(anonymous function) [as func] (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:122:17)
at process (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1352:20)
at argumentsToChain (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1361:12)
at Server.serverMethod [as put] (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1475:21)
my code for connection is as follow
server.listen(config.port, function() {
mongoose.connection.on('error', function(err) {
console.log('Mongoose default connection error: ' + err)
process.exit(1)
})
mongoose.connection.on('open', function(err) {
if (err) {
console.log('Mongoose default connection error: ' + err)
process.exit(1)
}
console.log(
'%s v%s ready to accept connections on port %s in %s environment.',
server.name,
config.version,
config.port,
config.env
)
require('./routes')
})
global.db = mongoose.connect(config.db.uri)
})
routes code
server.get('/', function indexHTML(req, res, next) {
fs.readFile(__dirname + '/../index.html', function (err, data) {
if (err) {
next(err);
return;
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(data);
next();
});
});
This was fine ... I changed something and now it stopped working with this error. The error is that it is not able to assert some function... in mongodb client. it needed a function. Is it asking to add some handler function? where to add that
Thank in advance
handler (func) is required is an error that is thrown by restify if one of your routes or middlewares is undefined.
For example:
server.put('/foo/');
This would also trigger it:
var myMidelware = undefined; // todo: define this
app.put('/route', myMiddleware, (req, res) => { /* todo: handle req */ })
That will throw the error handler (func) is required when it tries to validate that myMidelware is a function.
I don't see that in your posted routes code, but I think it's happening somehow. Do you have a PUT method defined somewhere?
(The same error would also happen with server.get(), server.post(), etc, but the [as put] in the stack trace indicates that it's choking on a server.put() call.)
See https://github.com/restify/node-restify/blob/v7.2.1/lib/server.js#L1386
Also, I don't believe the error has anything to do with mongodb; mongo is just in the stack because you run require('./routes') in the mongo connection open handler. The error is coming from your routes file. Annoyingly, mongo's error handling is loosing part of the stack trace. If you moved require('./routes') to outside of the mongo stuff, it would give you the proper stack trace.

Error cannot be caught with Express.js and gridfs-stream

It's an easy image (picture) download server, Express.js receives the request, gets an image from MongoDB GridFS, and responds with the file.
It's OK when request is valid (when the requested file exists).
The problem is that I cannot catch the MongoError when the query failed (i.e. requested image does not exist).
import Grid from 'gridfs-stream'
const root = 'fs_images'
// This func returns the file stream
export function getReadStream (id) {
const gfs = Grid(mongoose.connection.db, mongoose.mongo)
const options = {
_id: id,
mode: 'r',
root: root
}
const readStream = gfs.createReadStream(options)
readStream.on('error', function (err) {
// throw here
// it performs the same without this on-error hook;
// if comment the `throw err`, nothing will happens
// but I want the caller knows the error
throw err
})
return readStream
}
And this is the router
router.get('/:fileId', function (req, res, next) {
const fileId = req.params.fileId
try {
const imgReadStream = image.getReadStream(fileId)
imgReadStream.pipe(res)
} catch (err) {
// nothing catched here
// instead, the process just crashed
console.log(err)
}
}
And I just cannot catch the err. When I try to request something that doesn't exist the MongoError shows in the console, and the app crashes with errno is 1.
Head of console output:
/.../node_modules/mongodb/lib/utils.js:123
process.nextTick(function() { throw err; });
^
MongoError: file with id 123456123456123456123456 not opened for writing
at Function.MongoError.create (/.../node_modules/mongodb-core/lib/error.js:31:11)
This may be a bit different. If somewhere else throws an Error it will be caught by my error-handler (app.use(function(err, req, res, next){ /* ... */})), or at least by the default handler from Express.js, and returns a 500, without the process crashing.
In short, I want the app to know and catch this MongoError so I can handle it manually (i.e. return a 404 response).
try/catch won't work because the error is happening in a different tick (asynchronously). Perhaps you could listen for the error in the router instead?
const imgReadStream = image.getReadStream(fileId)
imgReadStream.on('error', function(err) {
// Handle error
});
imgReadStream.pipe(res)

How to get files list using fileSystem?

I am trying to get list of files in specified directory but its throwing error any idea what is implemented wrong in below code.
service.js
var fs = require('fs');
var path = require('path');
var async = require('async');
var filePath = path.resolve('./logs/dit/');
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
files.forEach(function(file) {
console.log(file);
});
})
}
checkSearchObj();
error
TypeError: Cannot read property 'forEach' of undefined
at C:\Users\sh529u\WebstormProjects\ulog\app\serverf
s:8:15
at FSReqWrap.oncomplete (fs.js:82:15)
Change this:
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
files.forEach(function(file) {
console.log(file);
});
})
}
to this:
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
if (err) {
console.log('Error:', err);
return;
}
files.forEach(function(file) {
console.log(file);
});
})
}
to see what's the error.
You need to check for errors instead of assuming that there was no error or otherwise you will try to access undefined variables and the only error you'll see is that you access undefined and not the actual error that's in err in this case.
Your callback will either get the err or the files but not both.
Update:
After changing the code to display error you got:
Error: { [Error: ENOENT: no such file or directory, scandir 'C:\Users\Web stormProjects\ulog\app\serverfiles\logs\dit'] errno: -4058, code: 'ENOENT', syscall: 'scandir', path: 'C:\\Users\\sh529u\\WebstormProjects\\ulog\\app\\serverfiles‌​\\logs\\dit'
Which means that the program doesn't display the files in the directory because there is no such directory. Mystery solved. Now you can see how useful it is to actually check the errors.

Categories