I have just started learning about MongoDB and I am trying to host my node js application locally via MongoDB Server 6.0 (without using mongoose or atlas)
I copied the async javascript code given in the MongoDB docs. I made sure to run mongod before executing the below code
MongoDB server started
const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"**mongodb://localhost:27017**";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("Connected successfully to server");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
It's throwing an error:
image of the error it's throwing
Problem is, the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1
However, net.ipv6 defaults to false.
The best option would be to start the MongoDB with this configuration:
net:
ipv6: true
bindIpAll: true
or
net:
ipv6: true
bindIp: localhost
Then all variants should work:
C:\>mongosh "mongodb://localhost:27017" --quiet --eval "db.getMongo()"
mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
C:\>mongosh "mongodb://127.0.0.1:27017" --quiet --eval "db.getMongo()"
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
C:\>mongosh "mongodb://[::1]:27017" --quiet --eval "db.getMongo()"
mongodb://[::1]:27017/?directConnection=true&appName=mongosh+1.6.0
If you don't run MongoDB as a service then it would be
mongod --bind_ip_all --ipv6 <other options>
NB, I don't like configuration
net:
bindIp: <ip_address>
in my opinion this makes only sense on a computer with multiple network interfaces. Use bindIp: localhost if you need to prevent any connections from remote computer (e.g. while maintenance or when used as backend database for a web-service), otherwise use bindIpAll: true
How to solve model.find() function produces "buffering timed out after ... ms"? I'm using mongoose v 5.11.0, npm v6.14.8 and mongodb v
Here's the code.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const assert = require('assert');
var mongoose = require('mongoose');
try {
var db = mongoose.connect('mongodb://localhost:27017', {useNewUrlParser: true, dbName: 'swag-shop' });
console.log('success connection');
}
catch (error) {
console.log('Error connection: ' + error);
}
var Product = require('./model/product');
var WishList = require('./model/wishlist');
//Allow all requests from all domains & localhost
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET");
next();
});
app.get('/product', function(request, response) {
Product.find({},function(err, products) {
if (err) {
response.status(500).send({error: "Could not fetch products. "+ err});
} else {
response.send(products);
}
});
});
app.listen(3004, function() {
console.log("Swag Shop API running on port 3004...");
});
The product model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var product = new Schema({
title: String,
price: Number,
likes: {type: Number, default: 0}
});
module.exports = mongoose.model('Product', product);
Additionally, running the file also produces the following warnings:
D:\Test\swag-shop-api>nodemon server.js
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
success connection
Swag Shop API running on port 3004...
(node:28596) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type function ([Function (anonymous)])
at validateString (internal/validators.js:122:11)
at Url.parse (url.js:159:3)
at Object.urlParse [as parse] (url.js:154:13)
at module.exports (D:\Test\swag-shop-api\node_modules\mongoose\node_modules\mongodb\lib\url_parser.js:15:23)
at connect (D:\Test\swag-shop-api\node_modules\mongoose\node_modules\mongodb\lib\mongo_client.js:403:16)
at D:\Test\swag-shop-api\node_modules\mongoose\node_modules\mongodb\lib\mongo_client.js:217:7
at new Promise (<anonymous>)
at MongoClient.connect (D:\Test\swag-shop-api\node_modules\mongoose\node_modules\mongodb\lib\mongo_client.js:213:12)
at D:\Test\swag-shop-api\node_modules\mongoose\lib\connection.js:820:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (D:\Test\swag-shop-api\node_modules\mongoose\lib\connection.js:817:19)
at D:\Test\swag-shop-api\node_modules\mongoose\lib\index.js:345:10
at D:\Test\swag-shop-api\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\Test\swag-shop-api\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (D:\Test\swag-shop-api\node_modules\mongoose\lib\index.js:1135:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28596) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:28596) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I tried increasing the bufferTimeoutMS or disabling the bufferCommands but still it won't work.
According to Documentation found in this link: https://mongoosejs.com/docs/connections.html#buffering
Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.
That's because mongoose buffers model function calls internally. This
buffering is convenient, but also a common source of confusion.
Mongoose will not throw any errors by default if you use a model
without connecting.
TL;DR:
Your model is being called before the connection is established. You need to use async/await with connect() or createConnection(); or use .then(), as these functions return promises now from Mongoose 5.
The issue on model.find() error: Operation products.find() buffering timed out after 10000ms" was resolved by removing the node_module folder, *.json files and reinstalling the mongoose module.
The issue on the warnings was resolved by following this instructions https://mongoosejs.com/docs/deprecations.html
Well, I encountered the same problem and had very similar code. I got the same error when sending a get request while testing.
Eventually, I found the solution that my localhost DB wasn't running at that moment. Though it's a foolish error, but I had a hard time finding it.
This error poped becuase you are trying to access models before creating the connection with the database
Always link your mongodbconnection file (if you have created) in app.js by
var mongoose = require('./mongoconnection');
or just keep mongodb connection code in app.js
For me was 100% MongoDB Atlas issue.
I've created a cluster in Sao Paulo that for some reason wasn't working as expected. I've deleted it, create a new one in AWS / N. Virginia (us-east-1) and everything started working again.
i'm using this function to connect to the db and avoid some warnings
mongoose.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, res) {
try {
console.log('Connected to Database');
} catch (err) {
throw err;
}
});
just use 127.0.0.1 instead of localhost
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
Or use family:4 in mongoose.connect method like that
mongoose.connect('mongodb://localhost:27017/TESTdb', {
family:4
})
.then(() => {
console.log('FINE');
})
.catch(() => {
console.log("BAD");
})
I had the same problem.
After a long search I was able to find it.
I created a new user in MongoDB atlas settings. I changed the MongoDB connection value with the new user.
Changing DNS setting to 8.8.8.8 or changing mongodb connection settings to 2.2.12 did not work.
In my case my i forgot to import db.config file in server.js file
There has been a change in mongoose v5^ the spaghetti code has been refactored, It now returns a promise that resolves to the mongoose singleton. so you don't have to do this.
// You don't have todo this
mongoose.connect('mongodb://localhost:27017/test').connection.
on('error', handleErr).
model('Test', new Schema({ name: String }));
// You can now do this instead
mongoose.connect('mongodb://localhost:27017/test').catch(err);
Check here for references
What's new in Mongoose v5^
If this doesn't work for you, you can then change your connection URL > Select your driver and version to v2.2.12 or later
First you should check in which port mongodb currently running.
Use this command to check that port
sudo lsof -iTCP -sTCP:LISTEN | grep mongo
If there you find different port rather than 27017, you should change it
I was having this issue only on deployed lambda functions and everything worked fine on my local. The following worked for me.
Delete node_modules folder.
npm install
commit/push the new package-lock.json file
merge / run cicd pipeline / deploy.
For me, the issue was node version. I was getting the same error with nodejs version 17.
After trying all the suggestions on this thread, stumbled upon this open issue. Tried downgrading node, but that did not work, finally uninstalled node 17 completely and installed node 16 and the problem was solved!
You can check your node version on Mac using node --version
This means that, mongo connection has not been established like others have mentioned, go through your code and see if perhaps you forgot to create a mongoConnect() function to connect with your atlas URI
the best way is to put your initialization in a function, connect to db before starting the server. use a combination of async and a condition to check if environment variables are there(incase db url is in env) here is a sample code.
const start = async () => {
if (!process.env.DB_URI) {
throw new Error('auth DB_URI must be defined');
}
try {
await mongoose.connect(process.env.DB_URI!, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
console.log('Server connected to MongoDb!');
} catch (err) {
throw new DbConnectionError();
console.error(err);
}
const PORT = process.env.SERVER_PORT;
app.listen(PORT, () => {
console.log(`Server is listening on ${PORT}!!!!!!!!!`);
});
};
start();
You should check if string connection is correct, because in my case I forgot to include the .env file in my proyect. This file contains string connection for my server in digital ocean.
MONGO_URI="mongodb+srv://server:gfhyhfyh.mongo.ondigitalocean.com/db_customers"
I have a few google cloud functions which make use of the redis memory store and it gives me this Redis connection to :6379 failed - read ECONNRESET at TCP. onread error every time any of function deployed. Previously I shared the createClient() code with all of the functions by creating a separate util file and including them on the CFs, I thought that was the issue. But please note that this Redis cache is working as expected other than this error.
Then I tried putting util code inside each of the google cloud functions which use the redis client to create the client. But I'm still getting this error from every cloud functions when every I deploy any of a cloud function. Even when deploying the functions that do not use the redis.
Here's how I create a client :
const bluebird = require('bluebird');
const redis = bluebird.promisifyAll(require('redis'));
const cache = redis.createClient({ port: REDIS_PORT, host: REDIS_HOST });
cache.on("error", (err) => {
console.log("API One - Redis cache error : " + err);
});
const list = async(data) => {
// Do something with data.
let cachedData;
if(cache.connected) {
await cache.hgetAsync(key); // Get cached Data.
}
// Do something with cached data if cachedData available.
if(cache.connected) {
await cache.hsetAsync(key, data); // Set Some Data.
}
return data;
}
module.exports = functions.https.onCall(list);
Why I'm seeing this error on every cloud function logs?
Sample error logs I get:
API One - Redis cache error : Error: Redis connection to <Ip Address>:6379 failed - read ECONNRESET
API Two - Redis cache error : Error: Redis connection to <Ip Address>:6379 failed - read ECONNRESET
Have you tried closing the redis connection before the function finishes?
The redis module may have background callbacks active during the life of the client, not closing the connection prior to function termination may be causing the connection to timeout when the cloud function terminates. Make sure that all asynchronous operations finish before the function terminates.
For example:
Example
Let me know if this works for you.
I am new to these languages, I'm working on a project that provides a management of a database of users and a function for each of them, by button, to send them emails with their code.
I'm using nodejs, expressjs (database) and sendgrid, these are their versions (I think they are the last ones):
"#sendgrid/mail": "^6.2.1",
"sendgrid-rest": "^2.4.0",
"express": "^4.16.2",
node v8.9.4
I am using windows 10 and through the official sengrid guides I have obtained the following code:
exports.sendMail = function(req,res){
var name = req.params.nome;
var email = req.params.email;
var code = req.params.code;
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey('api_key');
const msg = {
to: email,
from: 'a#b.com',
subject: 'event',
text: 'Dear '+name+',\n'+'this is your code: \n',
html: '<br><strong>'+code+'</strong><br>',
};
sgMail.send(msg).then(() => {
res.redirect('/users');
}).catch((error) => {
console.log('error', error);
});
}
I tried creating an environment variable via windows settings, but it did not work. I found another solution that proposed to create a .env file and to export a variable containing the api_key of sendgrid:
export SENDGRID_API_KEY = 'SG.xxx...'
but for the time being it does not seem to work, rather I receive these messages, and no e-mails are sent.
I checked the correctness of the api_key and it's the one that starts with SG.XXXXX... , so I think it's right.
Here are the messages:
(node:9688) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Provide at least one of to, cc or bcc
(node:9688) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
They are simple warnings, but I fear they affect the operation.
Another question: I read in some posts, that the use of api_key in a file in the project directory (I think they spoke of the public directory (/ project / public)), I put the file in the main directory of my project (/ project), has security problems because it could be seen externally, is it safe what I did? Do I have to move it to other directories?
Every help is welcome, I thought it would be much easier to use the sendgrid service in a web app.If there was a faster method through sendgrid it is welcome.
EDIT: Thanks to the answers I managed to arrive in part to a solution, modifying the code as above, I do not receive any error but it seems not to send any email to the mailbox. Some help?
try this, You are not handling the error condition. Error: Provide at least one of to, cc or bcc may be to email is null/undefined.
sgMail.send(msg).then(() => {
res.redirect('/users');
}).catch((error) => {
console.log('error', error);
});
I am on web3 1.0.0-beta.27, and I ran a private blockchain as:
geth --identity "node" --nodiscover --maxpeers 0 --datadir path/to/data --networkid 123 --ws --wsport 8546 --wsorigins "*" console
Then in a app.ts file I have:
import * as Web3 from 'web3';
var web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
web3.eth.getAccounts().then(accounts => {
var sender = accounts[0];
web3.eth.personal.unlockAccount(sender, 'password');
});
But I get error:
Unhandled rejection Error: Returned error: The method personal_newAccount does not exist/is not available
Searching online for this issue, I should have started the geth process with --rpcapi="db,eth,net,web3,personal,web3", however adding this flag does not help, even though rpc is just a kind of ipc correct?
Furthermore, on the geth console I am able to unlock the account with
personal.unlockAccount(sender, 'password')
You added personal to rpcapi, but are connecting through WS. You need to add it to wsapi.
rpc is just a kind of ipc correct?
The 3 connection protocols are IPC-RPC, JSON-RPC, and WS-RPC. The rpc* configuration parameters are for JSON-RPC (over HTTP), not IPC/WS.