I am trying to create a webpage that uses data from a MariaDB. My current idea (which has been giving me a lot of trouble) is to just connect to the database from the app.js file, which is the main script for my index.html.
const dotenv = require("dotenv");
dotenv.config();
const mariadb = require("mariadb");
const pool = mariadb.createPool({
database: process.env.DATABASE,
host: process.env.HOST,
user: process.env.USER_TOKEN,
password: process.env.PASSWORD,
});
// the rest of the code involves selecting from the db, and parsing the data
However, I have been running into many issues. I'm not too knowledgeable on all this, but I found that I need to webpack the file if I want to be able to use the "require" keyword. But I could not figure that out as I kept running into weird issues when using Browserify; I think there may be an incompatibility with MariaDB. I also looked into using JS modules, but I am not sure if that is possible with MariaDB.
I am trying to come up with another solution, potentially using some sort of API to a back end, which would make the GET request to the database, but I feel like it should not have to be that complicated for my sake (I also wouldn't really know where to start with this). All I basically want to do, is make a GET request to a MariaDB when the page loads on the client's browser and display that data on the webpage. Is there a simple way to do this?
I suggest you use nodejs to connect and query database as it will greatly resolve a lot of overhead for you..
The easiest way i can think of is using a prisma starter template here
https://github.com/prisma/prisma-examples/tree/latest/javascript/script
It also gives the added advantage of the ORM function...
Hope it helps.
I'm learning Node.js and just started working with MongoDB.
I'm making a connection with the MongoDB Cluster I've created
const dbURI = 'mongodb+srv://testuser:test1234#nodelearning.fzofb.mongodb.net/mydb?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => console.log('connected to db'))
.catch((err) => console.log(err));
When I run it nodemon app I get this error:
Error: querySrv ENOTFOUND _mongodb._tcp.mydb.fzofb.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:dns:206:19) { errno: undefined, code: 'ENOTFOUND', syscall: 'querySrv',
hostname: '_mongodb._tcp.mydb.fzofb.mongodb.net' }
Also if you have your mongodb Atlas cluster inactive after a while. It could get paused and then when you run a server it returns the error you had.
So another bet is to login to your Atlas account and confirm if it hasn't been paused. Well this option of mine should only be considered after trying the options above.
The error indicates that there is no error in the code. This leaves you with three potential possibilities:
Ensure you have MongoDB installed on your computer.
Make sure you're connected to wifi that is not public.
Make sure you have allowed the IP in network access of MongoDB as shown in the image below:
In my case, I was connected to public wifi in a coworking space. I change my connection to my personal hotspot and it worked.
Try disabling VPN if enabled. It helped me.
You need to convert special characters in connection string password to percent encoding.
I had the same issue with mongodb-compass. For me it was incorrect connection string format. So in my case it required to convert # symbol in my password to %40 (percent encoding).
You can read more about this in here. and here.
I had the same issue. I was using the latest srv string. I don't know if this will help you. But using the olderve
mongodb://<username>:<password>#cluster0-shard-00-00.2pznz.mongodb.net:27017,cluster0-shard-00-01.2pznz.mongodb.net:27017,cluster0-shard-00-02.2pznz.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-s5orlv-shard-0&authSource=admin&retryWrites=true&w=majority```
This error happens if the IP Address you try to access the database with is not on the IP Access List. Go to MongoDB Login and under security click on the Network Access and check if your IP is added there.
In my case, the problem got solved when I sign in to my MongoDB atlas account.
In my case, I was re-using a string connection from another project.
I copied my original string connection from MongoDB site and worked fine.
FIX : : previously my cluster password was "chaudhary#786" which contained the
special character "#", due to which the compass string connecting
compiler got confused hence in the next turn I changed my password to
"gghjkl98", i.e without any special characters. and I got connected
without any errors.
SOLUTION : password of the cluster should not contain characters like \backslash
or # or any other characters
I have server.js and db.js The db.js file interacts with my database using Mongoose and I use server.js to call functions from db.js :
var mongoose = require('mongoose');
mongoose.connect('', { useNewUrlParser: true })
var Schema = mongoose.Schema;
module.exports = function () {
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
return db.once('open', function() {
console.log("Connected to DB")
var postschema = new Schema({
title: String,
intro: String,
body: String,
author: String,
timestamp: { type: Date, default: Date.now }
});
var post = mongoose.model('post', postschema);
return {
newPost(title, intro, body, author) {
var newpost = new post({
title: title,
intro: intro,
body: body,
author: author
})
},
getPostsAll() {
post.find({}, function (err, res) {
return (`Error:${err} Posts:${res}`)
})
}
}
})
}
And my server.js calls three functions from db.js :
var DB = require('./db.js')
var db = DB()
db.getPostsAll()
db.newPost()
I don't understand why I get this error :
connection error: { MongoNetworkError: connection 4 to black-test-shard-00-01-ewyaf.mongodb.net:27017 closed
at TLSSocket.<anonymous> (E:\HTML\black-box\node_modules\mongodb-core\lib\connection\connection.js:276:9)
at Object.onceWrapper (events.js:272:13)
at TLSSocket.emit (events.js:185:15)
at _handle.close (net.js:541:12)
at TCP.done [as _onclose] (_tls_wrap.js:379:7)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
What am I doing wrong? I found an article but can't make anything of it.
I had
'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]:
I added my current IP to whiteList following "main page > security section > network access > add IP" in MongoDB website.
I hope this helps.
What is a TransientTransactionError
A TransientTransactionError is a transactional error that is classified as temporary, and if retried it may be successful. Furthermore, a TransientTransactionError write conflict occurs prior to a commit when no write lock has been taken and the transaction (new data) is not reflected in the transaction snapshot (previous data.) As a result, these errors are completely safe to retry until there is a successful commit.
Transactions that retry in this scenario are retried from the beginning of the transaction.
Keep in mind This error label is different than commit errors that happen when the lock has been taken but the transaction can't complete its commit. The error label for this is UnknownTransactionCommitResult. The reference to this is notable due to the difference in understanding where in your application an error is occurring and what may be the underlying cause and how the application can and or will respond due to different error types.
If you're using MongoDB supported drivers, there are two possible cause the code is getting this error:
Any database command error that includes the "TransientTransactionError" error label in the "errorLabels" field.
Any network error encountered running any command other than commitTransaction in a transaction.
The code example in MongoDB Transactions: retry-transaction show cased how to handle TransientTransactionError.
If the error message is MongoNetworkError, it means the transient transaction error is related to the network connectivity between the client and the server. This could either be a one time network glitch which is retry-able, or there is no network access which require network configuration. If the error is encountered on the first time the client trying to access the server, it is likely that there is network configuration needed. If the server is on MongoDB Atlas, please see Configure Whitelist Entries.
Go to your mongoDB Atlas Dashboard
Open Network Access (its there in side navbar)
Click on ADD IP ADDRESS
Click on allow from Any IP Address ( it basically give access to your dynamic IP address)
Now you are done.
If there is no security issue and you are just doing it for connecting:
While setting up the IP Whitelist; format should be 0.0.0.0/0 , you will not face the issue.
Moreover, as you have answered yourself, we can add the ip for which we need access.
I had the same issue/error, albeit on a Windows machine. Even though I thought I had started the mongodb service, I didn't see it running in Windows Services. So, I manually started the mongoDB service inside Services and then the error went away. Hope this helps!
I encountered this error when running the populatedb.js script in the MDN tutorial for Express/NodeJS.
The script was looking for a db connection starting with mongodb://, however my connection string from mongo started with mongodb+srv://.
I edited the script to check for this syntax instead, which resolved the error.
I hope this helps someone.
I had similar problem...
All day I was able to connect via mongoose. Then bang I started getting 'TransientTransactionError' error. I could connect to mongoDB via shell so I knew the server was up and running as expected.
IPv6/localhost. My IP switched from IPv4 to IPv6. I resolved the issue by disabling IPv6 and getting regular IPv4 IP.
EDIT -- seems I can reliably re/create this issue by connecting to 'localhost' while my NIC is configured with a IPv6 IP. Changing localhost->127.0.0.1 seems to resolve the issue.
Make sure the server didn't run out of storage space.
In my case, none of the suggested answers helped. I was pulling out my hair until I've noticed that the server ran out of storage space. Clearing up a few megabytes of storage solved the error immediately.
The TransientTransactionError actually makes sense in this scenario due to its nature of being a temporary error that could be resolved upon an explicit retry - though it did take me a while to figure out it had to do with a storage issue.
For me, the transient transaction error came whenever I switched from my wifi network to my phone's hotspot. If this happens to you too, go to MongoDB website where you have made your database and white list your current IP address again. This will solve your problem.
If you are using MongoDB Atlas. You need to Whitelist your IP address on the Atlas Console in the security settings.
If you still have this error, another reason is that you forgot to restart nodemon server after changing config file. CTRL+C and start again, that's how i solved it.
Security > Network Acess> Add IP Adress > Add Current Computer IP address.
Solved my problem.
Using mLab -- When your cluster is created, you need to add a database user (under the users tab) and make sure to not click the Make read-only checkbox. Once I did this, the errors went away. I was getting the same errors as above. The database user can be your login user name and password.
Go to your MongoDb Atlas dashboard. Click on Network Access, Click on add Ip Address and allow connectivity from any Ip Address.
This should solve your problem.
I had this problem when trying to connect my Heroku app to a MongoDB Atlas database.
If you do a on you terminal
heroku logs --tail
You might see
ERROR: { MongoNetworkError:
connection 4 to cluster0-shard-40-01-qnwp8.mongodb.net:27017 closed
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }`
After whitelisting the server connection on MongoDB Atlas, the database connection error was resolved.
You can read about the error online but the solution for this problem is: go to you MongoDB atlas and add your IP address.
Go to:
main page > security section > network access > add IP
This problem is basically when the architecture do not know your own IP address.
Also, copy-paste directly to google to get straight forward solution.
I am building a Node.js application, which reads from a MongoDB cluster. The application uses Mongoose to communicate with Mongo.
I would like to build a functionality, which is able to tell me, what does the mongoose know about the MongoDB replica set real-time (like calling rs.status()), but so far I was not able to find any kind of informations around the internet.
The purpose of this would be to be able to monitor, if something was changed in the replica set, and report it back if needed.
The problem is, I've found so far nothing on the internet in this subject. Does anyone have any idea on how to start it? It would be nice, if I could use the current mongoose connections for this purpose.
You can do this, but you need to be connected to the "admin" database and you will probably want a different connection for this other than what the rest of your application uses. Something like:
var mongoose = require("mongoose");
mongoose.connect(
"mongodb://localhost:27017,localhost:27018,localhost:27019/test");
var conn = mongoose.createConnection(
"mongodb://localhost:27017,localhost:27018,localhost:27019/admin");
conn.on("open",function() {
conn.db.command({"replSetGetStatus":1 },function(err,result) {
console.log( result );
});
});
It is important to wait for the connection to be established as well, hence the "event" callback. Mongoose will internally "queue" it's own methods operations until a connection is made, but this does not apply when grabbing a handle to the native db object and executing methods from that.
I'm trying to create a simple nodejs application that connects to the pathofexile.com/trade api.
The problem with this API is that is that you cannot use it unless you're logged in on the main website (my code works in the browser, but I'm trying to make it into a desktop application). There are several other applications that solves this issue by creating a session ID cookie with a users session ID (ID that you can get by logging in to the website). Unfortunately the documentation of the API is very limited and I havn't been able to find any information on how I can create/use the cookie as needed.
If I try to connect to the websocket, without being logged in to the main pathofexile website, I get the following error:
VM58:1 WebSocket connection to 'wss://www.pathofexile.com/api/trade/live/Metamorph/e602K4cL' failed: HTTP Authentication failed; no valid credentials available
I've tried using my sessionID to create a cookie like this by using the built in features in node:
const cookie = { name: 'POESESSID', value: '3acbf42fb842aasdqwe1a0c355f',domain:
'.pathofexile.com' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// success
console.log("Cookie set (?)")
}, (error) => {
console.error(error)
})
Unfortunately, this does not work. I'm very unfamiliar with websockets (only started playing around with any of this a few days ago by accident), and I'm even less familiar with how websockets access and get data from cookies.
I've tried other modules like npm cookie-parser, npm request and npm needle to no avail.
The closest I've gotten to an answer is from a one year old reddit post where the user used C# to get this to work.
This is the code used in that example:
// Setup HTTP connection
HttpClientHandler handler = new HttpClientHandler();
CookieContainer cookieContainer = new CookieContainer();
cookieContainer.Add(composeUrl, new Cookie("POESESSID", sessionId));
handler.CookieContainer = cookieContainer;
HttpClient client = new HttpClient(handler);
If someone could help shine some light on this I'd be very appreciative. I understand that this question is incredibly niche and perhaps I'm asking it in the wrong forum, but I really don't know where to turn.
Appreciate any help!
//Alex