after I started server at port 27017, opened another window and run my server program it is showing the connection error as follow
C:\node-mongodb\node_modules\mongodb\lib\server.js:242
process.nextTick(function() { throw err; })
^
AssertionError: { [MongoError: connect UNKNOWN 127.0.0.1:27017 -
Local(undefined:undefined)]
name: 'MongoError',
message: 'connect UNKNOWN == null
at C:\node-mongodb\simple-server.js:8:12
at C:\node-mongodb\node_modules\mongodb\lib\mongo_client.js:330:20
at C:\node-mongodb\node_modules\mongodb\lib\db.js:231:14
at null.<anonymous> (C:\node-mongodb\node_modules\mongodb\lib\server.js:240:9)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at null.<anonymous(C:\nodemongodb\node_modules\mongodb\node_modules\mongo
db-core\lib\topologies\server.js:218:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
I tried removing lock file repairing mongodb and other solutions that are given for other similar questions but they are not working.
please tell my why this error is coming and how I can resolve it.
I am running on windows xp(32-bit).
This is the simple-server.js file:
var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
assert.equal(err,null);
console.log("Connected correctly to server");
var collection = db.collection("dishes");
collection.insertOne({name: "nerd21", description: "test"},
function(err,result){
assert.equal(err,null);
console.log("After Insert:");
console.log(result.ops);
collection.find({}).toArray(function(err,docs){
assert.equal(err,null);
console.log("Found:");
console.log(docs);
db.dropCollection("dishes", function(err, result){
assert.equal(err,null);
db.close();
});
});
});
});
screenshot of command prompt is as follows:enter image description here
Related
The class:
const mysql = require('mysql');
module.exports = function () {
this.connection = mysql.createConnection({
host : 'localhost',
user : 'USER',
password : 'PASSWORD',
database : 'DATABASE',
multipleStatements: true
});
this.query = (sql, args) => {
return new Promise( ( resolve, reject ) => {
this.connection.query( sql, args, ( err, rows ) => {
if ( err )
return reject( err );
resolve( rows );
});
});
};
this.close = () => {
return async () => {
try {
this.connection.end(err => {
if (err) throw err;
return;
});
} catch(e) {
return e;
}
}
};
};
In index i call it like this:
const Database = require('./server/modules/mysql'),
connection = new Database();
The problem:
Overnight mysql crashes:
[nodemon] starting `node index.js`
listening on port 420
events.js:292
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\protocol\Protocol.js:112:13)
at Socket.<anonymous> (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\Connection.js:94:28)
at Socket.<anonymous> (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (events.js:315:20)
at Protocol._delegateError (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.end (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\protocol\Protocol.js:116:8)
at Socket.<anonymous> (C:\Users\fedesc\Sites\borsalino\node_modules\mysql\lib\Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
[nodemon] app crashed - waiting for file changes before starting...
I'm using Nodemon and a simple restart to the application solves the issue until the next morning (or next long period of not coding.)
I'm not really using close() anywhere or after anything since in the docs it says it's not needed, just query().
but clearly i get a timeout somewhere somehow, like i should deal with the opening and closing of connection.
Do i need to close connections after making queries in my app?
Is there a way to set the timeout limit or dealing with him?
Am i just setting it up wrong or using wrong/outdated tool?
Thanks.
So I'm new to node.js and I'm trying to make a connection to my local database to later insert data.
However when I execute my code it gives me a connection error with errorcode ESOCKET. I tried different ways to structure my connectionstring but it always gives me this error.
As I said I'm new to nodejs so I'm not really sure if the problem is my connection string or the port it uses (1433) or something entirely different.
code:
const sql = require('mssql');
const config = {
user: 'test',
password: 'test',
server: '.',
database: 'Parkings'
}
async function databaseconn(parking){
try{
let pool = await sql.connect(config);
await pool.request().query(`
if not exists (select 1 from [Parking] where [Name] = '${parking.name}')
begin
insert into Parking ([Name], [Type], [Latitude], [Longtitude], [MaxCap])
values ('${parking._name}', '${parking._type}', ${parking._latitude}, ${parking._longitude}, ${parking._maxcap})
end
`);
await pool.request().query(`
insert into [Entry] ([TimeDay], [Available], [ParkingId])
values (${p._time}, ${p._available}, (select Id from Parking where Name = '${p._name}'))
`);
pool.close();
} catch (err){
console.log(err);
}
}
error:
ConnectionError: Failed to connect to .:1433 - getaddrinfo ENOTFOUND .
at Connection.<anonymous> (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
at Object.onceWrapper (events.js:428:26)
at Connection.emit (events.js:321:20)
at Connection.socketError (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connection.js:1290:12)
at D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connection.js:1116:21
at GetAddrInfoReqWrap.callback (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connector.js:158:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
code: 'ESOCKET',
originalError: ConnectionError: Failed to connect to .:1433 - getaddrinfo ENOTFOUND .
at ConnectionError (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connection.js:1290:56)
at D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connection.js:1116:21
at GetAddrInfoReqWrap.callback (D:\Documenten\2019-2020\ParkingProject\nodejs_serverapp\node_modules\tedious\lib\connector.js:158:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
message: 'Failed to connect to .:1433 - getaddrinfo ENOTFOUND .',
code: 'ESOCKET'
},
name: 'ConnectionError'
}
picture
SOLUTION
after a lot of trouble I fount the sollution to the issue thanks to a github post. You have to enable the TCP/IP protocol on your sql server and restart it. Guide in links below:
https://github.com/tediousjs/tedious/issues/799
https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm
Each time I try to connect to my server to access the documents in the database, it comes back with a connect ETIMEDOUT error.
I own the server, running Mongo 4.0, have run mongod and left running, there is data in the collection.
let MongoClient = require('mongodb').MongoClient;
let uri = "mongodb://notmyrealusername:notmyrealpassword#thomas.misson.info/tom-portfolio";
MongoClient.connect(uri, { useNewUrlParser: true }, async function(err, db) {
try{
if (err) throw err;
let res = await db.collection("projects").find().count()
console.log(res);
db.close();
}
catch(err)
{
console.log(err);
}
});
Error message:
{ MongoNetworkError: connect ETIMEDOUT 94.195.191.104:27017
at Socket.err (H:\Documents\Personal projects\node_modules\mongodb-core\lib\connection\connect.js:287:16)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I have opened the port using netsh advfirewall firewall add rule name="Open mongod port 27017" dir=in action=allow protocol=TCP localport=27017
Have now added new interface to the mongod.cfg file.
# network interfaces net:
port: 27017
bindIp: 127.0.0.1, 94.195.191.104
I get an error when requesting an API and it crashes on any win server. Can anybody help, please?
This is part of my code:
app.get("/js/2806/api/products/getAllDrugs", (req, res) => {
const connection = getDBConnection()
const queryString = "SELECT * FROM tbl_drug";
connection.query(queryString, (err, rows, fields) => {
if (err) {
console.log("Failed to query for drugs: " + err);
// res.sendStatus(500);
res.json({
ErrorDetail: {
code: -1,
msg: err.code
}
});
res.end();
return
}
const drugs = rows.map((row) => {
return {
id: row.id,
storeId: row.drugStoreId,
drugName: row.drugName,
description: row.drugDsc,
drugUsing: row.drugUsing,
drugDoseId: row.drugDoseId,
categoryId: row.categoryId
};
})
res.json({
ErrorDetail: {
code: 0
},
Response: drugs
})
})
});
Error: Connection lost: The server closed the connection. at Protocol.end (C:\Users\Administrator\AppData\Local\CaptainCure\n s\mysql\lib\protocol\Protocol.js:112:13) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:97:28) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:502:10) at Socket.emit (events.js:194:15) at endReadableNT (_stream_readable.js:1125:12) at process._tickCallback (internal/process/next_tick.js:63:19) Emitted 'error' event at: at Connection._handleProtocolError (C:\Users\Administrator\AppData\ ainCure\node_modules\mysql\lib\Connection.js:425:8) at Protocol.emit (events.js:189:13) at Protocol._delegateError (C:\Users\Administrator\AppData\Local\Ca node_modules\mysql\lib\protocol\Protocol.js:390:10) at Protocol.end (C:\Users\Administrator\AppData\Local\CaptainCure\n s\mysql\lib\protocol\Protocol.js:116:8) at Socket. (C:\Users\Administrator\AppData\Local\Captain odules\mysql\lib\Connection.js:97:28) I— lines matching original stack trace ...] at process._tickCallback Cinternal/process/next_tick.js:63:19> [nodemon I - wait in is (nodemon] si ing 'node app.js -erver is running on port: 3000 ode events.js:174 throw er; // Unhandled 'error' event is (nodemon] starting 'node app.js' -erver is running on port: 3000
Because of the server is already running on back so we do have to restart.
and if your using both iterm and the terminal so you need to make sure that the you use only one other one you have to close.
I am trying to make a simple connection between my nodejs server and my mysql db (using Microsoft SQL server manager studio v14), below is my code and the error message appearing in my console window.
here is my code:
var express = require('express');
var app = express();
var sql = require("mssql");
app.get('/', function (req, res) {
// config for your database
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX'
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from Schools', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
The error I am getting looks like this:
Server is running..
tedious deprecated The default value for `options.encrypt` will change from `false` to `true`. Please pass `false` explicitly if you want to retain current behaviour. node_modules\mssql\lib\tedious.js:212:23
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at Connection.tedious.once.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:275:13)
at Connection.emit (events.js:182:13)
at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:14)
at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
code: 'ESOCKET',
originalError:
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at ConnectionError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\errors.js:12:12)
at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:30)
at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:174:19)
message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
code: 'ESOCKET' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1299:37)
at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1242:12)
at C:\Users\smr09\Desktop\Code\ou\db_test\test.js:24:17
at _poolCreate.then.catch.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:178:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
I am rather new at this particularly with dealing with databases. Can someone explain what the error means?
Add this line to the config:
options: {
encrypt: false
}
It will finally look like:
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX' ,
options: {
encrypt: false
}
};
try to put encrypt: false in your config{}, it will work.
your error:
ConnectionError: Failed to connect to localhost:1433 - Could not
connect (sequence)
My problem was resolved by adding port
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX',
port: '',
};