I am using Gulp building system. When I try to connect to my local MongoDB using mongoose I got error mongoose.connect is not a function.
Connection code:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
Your code seems fine. Are you sure you correctly installed mongoose?
npm install --save mongoose
Your problem isn't that you're using mongoose directly in react ?
Mongoose is a node module: "elegant mongodb object modeling for node.js".
Related
I'm using NodeJS with MongoDB using MongoDB package. When I run mongod command it works fine and gives "waiting for connection on port 27017". So, mongod seems to be working. But MongoClient does not work and gives an error when I run node index.js code.
The error is given below in the picture.
I have installed mongo DB 5.0.5, mongoose package 6.1.13 and my code is-
// getting-started.js
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
}
I'm able to connect the database by console log and windows power shell but not by the node.js
Error description
the problem seems to be with mongoose & mongodb packages as it works fine when
mongoose.connect('mongodb+srv://mydb:<password>#cluster0.w1opr.mongodb.net/test?retryWrites=true&w=majority');
is removed
it also works fine on repl.it cloud env
here is my code
var express = require('express');
var ejs = require('ejs');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb+srv://mydb:<password>#cluster0.w1opr.mongodb.net/test?
retryWrites=true&w=majority');
app.set('view engine','ejs')
app.use(bodyParser.urlencoded({extended: true}));
.
.
.
app.listen(3000,function(){
console.log('Server is running on port 3000');
});
Actually mongoose 6 requires Node 12 or higher, so this is expected behavior. Mongoose 6 does not support Node 10.So updating Node version will fix the issue. It also fix the problem by downgrading mongoose version to 5.
Check your node version, if it's lower than 12 it won't work, if that's the case updating node should do do the job. You could downgrade your mongoose version too.
There's an issue closed on Mongoose github page.
https://github.com/Automattic/mongoose/issues/10638
To get rid of this error in Windows upgrade to the latest version of Node.js by visiting this site https://nodejs.org/en/download/ and downloading the latest version of Node.js else if you do not want to upgrade to the latest version then you can get rid of this error by adding this line at the top of the file in node_modules/whatwg-url/dist/encoding.js:
const {TextDecoder, TextEncoder} = require("util");
Had the same problem when using tests. Setting the testEnvironment to node in my jest config fixed it (https://mongoosejs.com/docs/jest.html)
module.exports = {
testEnvironment: 'node'
};
For me i'm using UBUNUTU 20.04, as #MuhammadTariq said my node version was 10 and after upgrading the version to 16 the error is gone.
I am setting up an environment with mongodb, node.js, express.js and mongoose. mongoDB is in its own container, and the same with the rest.
It all worked fine until I tried to add mongoose.
After this line was added
const mongoose = require("mongoose");
I got an error-message when a ran docker-compose up
Error-message
This is my package.json
Package.json
This is my dockerfile for the api
Dockerfile
Does anyone knwo how to fix this?
I try to use Sequelize .. And I have problems :( . I don't know if I have a conflict with an other npm package ..
Like the tuto, I did :
npm install sequelize --save
npm install mysql2 -- save
In my react app, in "sequelizeYes" folder, I did :
import * as Sequelize from 'sequelize'
const seq = new Sequelize('galadat', 'root', '')
seq
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
export default seq
In app component, i call the file like this :
import('../sequelizeYes')
You can see on the picture differents errors in the console..
Do you have an idea ?
Did you require the module?
var Sequelize = require('sequelize');
var fs = require('fs');
If you shared the lines of code that the errors indicate it might help as well. It looks like several different files are having problems.
Sequelize is a nodejs module intended to be used in your backend. (server-side) It will not work on client-side (with libraries such as React) as it has dependencies to native NodeJS modules as based on your description that is what you're trying to do. If you're working on a web application and use for example express have a look here https://github.com/sequelize/express-example
As #Eric suggests, you should require sequelize on top of your code. So this should work:
var Sequelize = require('sequelize');
If the error persists, you should delete folder "node_modules" and reinstall dependencies using the command "npm install". Also you should make sure all errors are thrown because of sequelize import.
If the error still persists, you should make sure you try to manipulate the models in server-side code, as #archansel and #frenzzy suggest here: https://github.com/kriasoft/react-starter-kit/issues/976
The reason is referenced above by #razakj answer
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.