Error while inserting file to MongoDB from Node.js - javascript

I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB.
Can someone please verify whats the issue with my code is.
When I run the js file, I am getting following error:
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
Code is as follows:
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var currentTimeStamp = new Date();
var file = require (__dirname + '/functions');
mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
function (err, client) {
if (err) throw err;
client.createCollection('lbTopology' , function (err, collection) {
if (err) throw err;
collection.insert(file, 'lbTopology' , function (err) {
if (err) throw err;
client.close(function (err) {
if (err) throw err;
console.log('done');
});
});
});
});
Can someone please let me know what the issue is? Thanks a lot in advance

It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?

Related

fs Error: EISDIR: illegal operation on a directory, read

I'm getting "Error: EISDIR: illegal operation on a directory, read" after trying to read the content of a .json. This is how I'm trying to access the file. I'm using the FileSystem of node js.
fs.readFile( path, ( err, fileData) => {
if (err) {
throw err;
}
else {
return fileData;
}
});
While debugging I can see that the error is thrown before the if statement.
Any idea?
Maybe the path to the file is not the right one, make sure the path of your file looks like the one that appears in the following code:
const fs = require('fs');
fs.readFile('PATH_TO_YOUR_FILE/File_Name.json', (err, fileData) => {
if (err) {
throw err;
} else {
console.log(JSON.parse(fileData));
}
});

Copy file to new location

I'm trying to copy a file with the data inside a new file in a folder.
I tried doing this but it didn't work:
copyFile(`./data/guilddata/guilds/default/GUILDID.json`, `./data/guilddata/guilds/${guild.id}/GUILDID.json`, (err) => {
if (err) throw err;
});
https://www.npmjs.com/package/fs-copy-file
Does anyone know what to do? (${guild.id} just means the guild id, the folder is already there). I also get no errors. Thank you
const fs = require('fs');
fs.copyFile('./data/guilddata/guilds/default/GUILDID.json', './data/guilddata/guilds/' + guild.id + '/GUILDID.json', (err) => {
if (err) throw err;
console.log('All done! The file is copied!');
});

Node.js raises error when trying to make directory using mkdirp

I am trying to download and later serve images using Node.js. I need to save each image in a directory specified by the url. My code for downloading images gets stuck because there is no directory to save them to. I am trying to create one using mkdirp but keep getting the error [Error: EACCES: permission denied, mkdir '/20110'] errno: -13, code: 'EACCES', syscall: 'mkdir', path: '/20110'. Here is my full code:
controller.serveImages = function(req, res, connection) {
var file = "/" + req.params.attachmentId + "/" + req.params.attachmentFileName;
console.log("serve called", file);
var fs = require('fs'),
request = require('request');
var mkdirp = require('mkdirp');
mkdirp( "/" + req.params.attachmentId, function (err) {
if (err) console.error(err)
else console.log('Directory made!')
});
fs.readFile(file, function(error, data) {
console.log("reading");
if(error){
if (error.code === 'ENOENT') { //File not downloaded
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
download('https://www.google.com/images/srpr/logo3w.png', file, function(){
console.log('done');
});
}
else{
console.log(error)
}
}
else{
console.log("Found locally", data, file);
res.sendFile(file);
}
});
};
The code is adapted from here. An example request would be /20110/TNCA+PB.png. With that I would want to create a directory /20110 and save TNCA+PB.png there.
Seems that you're trying to create the folder at File System root level. Have you tried to create the folder under the application folder instead? Writing to root level is never a good idea.
Here are some suggestions to get the app running path Determine project root from a running node.js application , then you can append that to your mkdirp function.
Regards

NodeJS how to read from /dev/pts/1

Is it possible to read from /dev/pts/1 with nodeJS?
If i cat /dev/pts/1 i can see data.
I tryed to use this :
var fs = require('fs');
fs.readFile('/dev/pts/1', function(err, data) {
if (err) throw err;
console.log(data);
});
This give no output.
And also no error.
Image of running the script with no output

Check what files are present in remote directory with grunt

I'm looking for a way to check which files are present in a remote directory i want to access via ssh or similar and write the filenames into an array.
So far I had no luck. unix rsync has an -n flag which can print every file which is present at the destinated location, but I don't get how to use the rsync-output in grunt.
Here's how you might do it via sftp with ssh2:
var SSH2 = require('ssh2');
var conn = new SSH2();
conn.on('ready', function() {
conn.sftp(function(err, sftp) {
if (err) throw err;
sftp.readdir('/tmp', function(err, list) {
if (err) throw err;
console.dir(list);
conn.end();
});
});
}).connect({
host: '192.168.100.100',
port: 22,
username: 'frylock',
// password: 'foobarbaz',
privateKey: require('fs').readFileSync('/here/is/my/key')
});

Categories

Resources