node js mongoose mongoDB - node.js

Using phpstorm, trying to get mongoDB server up and running using nodeJS, i have mongo installed and have created the required folders on the c drive to store database, proceeded by going to command prompt and entering mongod, to get mongo db running....
checked npm ls,mongoose and mongo are listed fine, and are also present in my package json file.
when i try to run the index file from commmand prompt 'node index.js' i get the following error, see pic for details, i have included, the code used for setting up mongo... i have disable mongoserver from phpstorm repository, and use the one from the mongodb website
"mongodb": "3.0.0-rc0",
"mongoose": "^5.0.0-rc2",
var mongoose =require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localhost/quaver');
mongoose.connection.once('open', function(){
console.log('connection has been made , and is running')
});
mongoose.on('error', function(error){
console.log('Connection error',error);
});

error sorted by omitting the following lines from the semver.js file, in the node modules folder in the current project
line 203,204
//re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
//nodevar comparatorTrimReplace = '$a$2$3';
and line 232
// re[i] = new RegExp(src[i]);
mongoose now up and running

Related

How to resolve the `at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)` and server selection error

I have been trying to connect database locally to mongodb using nodejs but there are some errors i'm facing some errors while doing that.
In my main folder there is only one connection file named as db.js and other files are package.json and node_modules. This is error I got after running the node db.js at particular location I have installed mongoose also using npm command.
and after using the command node --trace-deprecation I got the following error log.
Here's the code of db.js:-
const mongoose=require('mongoose');
mongoose.connect('mongodb://localhost:27017').then(()=>{
console.log(`conncn established!!\n`);
}).catch((err)=>{
console.log('no connection',err);
})
// module.exports=mongoose;
// "mongodb://localhost:27017/Blogs_db -->it is the connection string for Blogs_db tried to but this //string also but it is not working
I have checked whether my server is running or not and tried to make the new folder and to follow the process again but it still shows the connection error
Go the answer and it is simple just to replace the local host in the connection string with 0.0.0.0 and it worked[Heres the output after replacing the localhost in above code with 0.0.0.0]

How to connect Mongo DB in a webpack dev server

I would like to connect to Mongo DB using a webpack dev server. While the connection using the node mongodb driver and configuring in server.js is direct and straight forward, I am thinking of a way to do the same using webpack dev server in development (mainly for the hot loading advantage).
I understand that there is a way of achieving the same using a webpack middleware, but is there another easier and better way of doing it.
webpack dev-server is generally a simple Express or similar node.js server. It's essentially exactly the same as writing is on an express.js server.
npm install mongoose mongodb --save-dev
const mongoose = require('mongoose'); // Replace with import as desired
const mongoConnectString = 'mongodb://localhost/database-name-here';
mongoose.connect(mongoConnectString, (err) => {
if (err) {
console.log('Err, could not connect to the database.');
}
});
Replace the mongoConnectString as needed for developing or using databases that aren't local to your machine.

Different Insert response from mongodb nodejs native driver on OSX and Windows

I'm getting different responses when inserting documents from Windows and OSX to an external MongoDB database.
Both systems are using mongodb driver v2.1.11 from https://www.npmjs.com/package/mongodb
Inserts are working fine on both but the result I get back is quite different.
OSX: { result: { ok: 1, n: 1, ...other data}, ops: [the inserted records] }
Win: [the inserted records]
Any idea why this is happening?
Steps to reproduce:
nodejs: 5.7.0
npm: 3.6.0
mongodb (from npm): 2.1.11
Given a nodejs application, install v2.1.11 of the official MongoDB driver for nodejs:
npm install --save mongodb#2.1.11.
From your nodejs application, import mongo, create a mongo client and connect to a MongoDB instance and insert a document into a collection:
import mongo from 'mongodb';
const MongoClient = mongo.MongoClient;
MongoClient.connect(mongoUrl, (err, db) => {
db.collection('someCollection').insert({ foo: 'foo'}, (insErr, result) => {
console.log(result); // Observe the result shape is different on Win/OSX
});
});
It turns out there was a package.json buried deep in my application that contains a lower version of the MongoDB driver.
Removing this pacakge.json and ensuring that everything was kept in the top level package.json has solved the problem for me.

Local Node.js script not connecting to mongoDB database

I setup a MongoLab MongoDB database through Heroku and am able to connect to it in the shell by issuing the below command:
mongo ds061701.mongolab.com:61701/heroku_app35721468 -u <dbuser> -p <dbpassword>
I'm using Node.js and Express and wrote the following code in a script.js file, which sits in the same directory as a node_modules folder containing mongoose.
var mongoose = require('mongoose');
mongoose.connect('mongodb://<dbuser>:<dbpassword>#ds061701.mongolab.com:61701/heroku_app35721468');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', function(){
console.log('db connected');
});
When navigate to the directory of this script in Terminal and type node script.js, I get the following error message:
dyld: lazy symbol binding failed: Symbol not found: _node_module_register
Referenced from: /Users/Jack/Documents/node-express-101/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/build/Release/bson.node
Expected in: dynamic lookup
dyld: Symbol not found: _node_module_register
Referenced from: /Users/Jack/Documents/node-express-101/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/build/Release/bson.node
Expected in: dynamic lookup
Trace/BPT trap: 5
What am I doing wrong here and how can I connect to my MongoDB database hosted on MongoLab with mongoose?
Referring to comments from OP's original question: the solution was the original dependency install did not complete successfully. Reinstalling solved it.
That error will be thrown when you have installed modules (that require compilation) with Node 0.12, but subsequently try to run those modules with an older Node version (like 0.10).

Node.js and Mongodb

So i am trying to use Node.js and Mongodb together and the goal is to use Node to get information and store it in a database with Mongodb. SO I have both Node and Mongdb intalled, and I install the Mongodb package with npm, this is the package mongodb recommends. But the problem I am having is that when I try to do
MongoClient.connect("mongodb://localhost:3000/exampleDb", function(err, db) {
if(err) { return console.dir(err); }else{
var collection =db.createCollection('test', function(err, collection) {}); }});
and I go to localhost:port_for express_server, but when the above code is supposed to run I get [Error: failed to connect to [localhost:3000]] in the Node console.Am I supposed to be running mongodb in the background or how is this supposed to work?
when you do
npm install mongodb
you only install a node.js client driver for mongodb.
in order for you script to run, you need to install and start a mongodb server on your box
check server installation procedures on http://docs.mongodb.org/manual/installation/
You seem pretty lost on what mongodb is and how to use it. Mongodb is a noSQL database.
Am I supposed to be running mongodb in the background
Yes, like mysql, you need the server to be installed and running, to use it. You need to do:
mongodb (the db server)
install server
start the server by typing mongod on terminal
check with mongo if it is running and you can connect to it.
node.js (the web server)
install node
install mongodb package
now test your code

Resources