Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: net.createConnection is not a function #1780

Closed
limone-eth opened this issue Dec 5, 2017 · 4 comments
Closed

Uncaught TypeError: net.createConnection is not a function #1780

limone-eth opened this issue Dec 5, 2017 · 4 comments
Labels

Comments

@limone-eth
Copy link

I'm trying to create a script which inserts data on my MongoDB collection when I submit an HTML form.
So I'm using browserify to use node module 'mongodb', but my bundle.js file (generated with browserify) throws this error on my browser:

bundle.js:8695 Uncaught TypeError: net.createConnection is not a function
    at Connection.connect (bundle.js:8695)
    at Pool.connect (bundle.js:9929)
    at Server.connect (bundle.js:16913)
    at Server.connect (bundle.js:35356)
    at createServer (bundle.js:33747)
    at bundle.js:33977
    at parseHandler (bundle.js:36014)
    at module.exports (bundle.js:35916)
    at connect (bundle.js:33938)
    at connectOp (bundle.js:33323)

I have a script.js that require('mongodb'), so I used this cmd line:
browserify -r mongodb script.js -o bundle.js

Any suggestion?

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Dec 5, 2017

the net module is not available with browserify because it does low-level networking, which is not possible in the browser. as a result mongodb can't be accessed directly from a browser. (direct access could also be unsafe, since javascript-savvy users might be able to change the query the form executes to something that eg. deletes the entire database.)

one way to approach this would be to create a node server that connects to mongodb, and exposes an HTTP API using something like express. then your client-side script could submit the form data to that HTTP API, which in turn would insert the data into mongodb.

@limone-eth
Copy link
Author

Do you mean something like this?

`var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongodb = require('mongodb');

var dbConn = mongodb.MongoClient.connect('mongodb://localhost:27017');

var app = express();
console.log("Connection Success");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.resolve(__dirname, 'public')));

app.post('/post-feedback', function (req, res) {
dbConn.then(function(db) {
delete req.body._id; // for safety reasons
db.collection('feedbacks').insertOne(req.body);
});
res.send('Data received:\n' + JSON.stringify(req.body));
});

app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0' );`

@goto-bus-stop
Copy link
Member

yeah! something like that should work i think.

@limone-eth
Copy link
Author

ok thanks, I will try to fix it with this method!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants