I'm trying to make a connection to MongoDB using Mongoose. But it does neither throw a error nor connect to the database. Following is my code.
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
console.log('Hi, there!!');
mongoose.connect('mongodb://localhost:27017/db_name', (err) => {
console.log('Callback called');
if(err) throw err;
console.log('Connected to database');
})
In the above code none of the console.log inside the callback do happen. But any place outside the mongoose.connect do work like console.log('Hi, there!!')
Versions Used
express: 4.0.0
mongoose: 3.8.40
node: 7.7.3
mongodb: 3.4.0
Using mongoose: 3.8.40 I got this in the console :
{ Error: Cannot find module '../build/Release/bson'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/kevin/nemeacreation/sites/test/stackoverflow/node_modules/bson/ext/index.js:15:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Hi, there!!
upgrading to "mongoose": "~4.4" fixed it for me. I got the answer here : https://stackoverflow.com/a/35516644/2829540
For info the latest release of mongoose is 4.10.4
Related
I try to start a project but when I try to connect the mongo with the server I encounter an error that shows that the connection is not working because it can not find the folder where the connection string.
my db file:
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI')
const connectDB = async () => {
try{
await mongoose.connect(db);
console.log('MongoDB connected..');
}catch(err){
console.error(err.message);
process.exit(1)
}
}
module.exports = connectDB;
my defualt.json:
{
"mongoURI": "mongodb+srv://<username>:<password>#devconector-
zfloj.mongodb.net/test?retryWrites=true&w=majority"
}
the error:
WARNING: No configurations found in configuration directory:C:\Users\Yair
Azaria\Desktop\תכנות\node.js\DevConnector\config
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the
environment.
C:\Users\Yair
Azaria\Desktop\node.js\DevConnector\node_modules\config\lib\config.js:203
throw new Error('Configuration property "' + property + '" is not defined');
^
Error: Configuration property "mongoURI" is not defined at Config.get(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\node_modules\config\lib\config.js:203:1)
at Object.<anonymous>
(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\config\db.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous>
(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\server.js:2:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
you need to have a config folder on the root level of your project and a default.json file in that config folder.
In the default.json file, have your mongoURI value set up. And with that you should be able to have a value for mongoURI when you access it in your program (that you are doing now as shown in your code).
$ npm install config
$ mkdir config
$ vi config/default.json
{
"mongoURI": ""mongodb://userID:password#mongoBox:27001/myMongoDB"
}
look in this link for more information https://www.npmjs.com/package/config
So the issue is that you did not properly spell "default", you spelt it as "defualt.json" instead of "default.json"
I guess you are trying to get the dbURI from config.json file which does not exist in the folder. In your case you have named it as default.json so just replace the file name in the import section of your db.js and it would work fine. As shown below
const mongoose = require('mongoose');
const defualt = require('defualt');
const db = defualt.mongoURI;
const connectDB = async () => {
try{
await mongoose.connect(db);
console.log('MongoDB connected..');
}catch(err){
console.error(err.message);
process.exit(1)
}
}
Let me know the results!
You have created config folder inside "src" folder. kindly move the config folder one directory out/up.
Am using node Active directory for validating users. Here is my node code:
const express = require('express');
const async = require('async');
const activedirectory = require('activedirectory');
var underscore = require('underscore');
const ldap = require('ldapjs');
const bunyan = require('bunyan');
const app = express();
app.get('/',(req,res) => {
var config = { url: 'ldap://10.100.1.10',
baseDN: 'dc=HBCAudit,dc=COM',
username: 'traveldesk.dev',
password: '$oftvision#123' }
var ad = new activedirectory(config);
});
//Port Number
const port = 7000;
//CORS Middleware
app.use(cors());
//Start Server
app.listen(port, () =>{
console.log('Server started on port '+ port);
});
But am getting error in command prompt like this:
c:\spitravel>node app.js
module.js:471
throw err;
^
Error: Cannot find module 'once'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\spitravel\node_modules\ldapjs\lib\client\client.js
:8:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I don't know why am getting this error. Am new to NODEJS.
Its due to depencies of ldapjs module, please run npm install ldapjs and recheck
I'm trying to run a gulp based application.
On Ubuntu, everything is working, but on Debian server not.
Ubuntu: npm: 5.3.0, nodejs: v8.2.1, bower#1.8.0, gulp-cli#1.4.0, npm#5.3.0
Debian: npm: 5.3.0, nodejs: v8.2.1, bower#1.8.0, gulp-cli#1.4.0, npm#5.3.0
Gulpfile:
'use strict';
const gulp = require('gulp');
const sass = require('gulp-sass');
gulp.task('scss2css', function () {
gulp.src(['./web/scss/style.scss'])
.pipe(sass({outputStyle: 'compressed'}))
.pipe(gulp.dest('./web/css/'));
});
gulp.task('scss2css:watch', function () {
gulp.watch('./scss/**/*.scss', ['scss2css']);
});
gulp.task('default', function () {
gulp.start('scss2css');
});
When I type "gulp", I receive message like below:
module.js:487
throw err;
^
Error: Cannot find module 'camelcase'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/gulp-cli/node_modules/yargs/lib/parser.js:3:17)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
I just removed nodejs package and directories:
/usr/lib/node_modules/
/usr/local/lib/node_modules/
'use strict';
var MongoClient;
MongoClient = require('mongodb').MongoClient();
MongoClient.connect(
'mongodb://127.0.0.1:27017/accounting',
function (err, connection){
var collection = connection.collection('customers');
collection.insert({'name': 'John Doe'}, function(err, count){
collection.find().toArray(function(err, documents){
console.dir(documents);
connection.close();
});
});
});
Getting this error when using this code, I would like to know what is causing the error and any possible fixes.
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\Matt\WebstormProjects\keyword-wrangler\index.js:6:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Not entirely sure what is causing this, I am using WebStorm and have installed the latest version of mongodb.
Correct way
var MongoClient = require('mongodb').MongoClient; // it's not a function
Your way
var MongoClient = require('mongodb').MongoClient();
Docs
I'm new in using full stack to build an application, i tried to create a connection that allow to the client side to connect to my mongo database. here my server.js code file
console.log("Server running...!");
var mongo=require('mongodb').MongoClien,
client=require('socket.io').listen(8080).sockets;
mongo.connect('localhost:27017/chat',function(err,db){
if(err) throw err;
client.on('connection',function(socket){
console.log('someone has connected !');
//waiting for input
socket.on('input',function(data){
console.log(data);
});
});
});
when i run this with node.js i get the following error in my command prompt window.
Server running...!
C:\Users\azus\Desktop\Psirt\code-master\server.js:6
mongo.connect('mongodb://127.0.0.1:27017/chat',function(err,db){
^
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:7
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
I checked my mongo server and it's running !!
I am working with the latest up to date version, i'm doing this on windows.
'MongoClien' is unknown
You are trying to end a line with a comma
Change
var mongo=require('mongodb').MongoClien,
to:
var mongo = require('mongodb').MongoClient;