I'm having a problem figuring out how to connect to the MongoDB service from my javascript file. It's been so frustrating I've been banging my head against the wall for a week now and can't figure out a solution. Please help.
I'm running Node.js and React - and am trying to connect to MongoDB to do basic CRUD functions.
package.json
{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"dependencies": {
"babel-core": "^6.17.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.16.0",
"babel-preset-react": "^6.16.0",
"mongodb": "^2.2.22",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.16.2"
},
"devDependencies": {
"css-loader": "^0.26.1"
},
"scripts": {
"start": "webpack-dev-server --hot"
},
"author": "",
"license": "ISC"
}
webpack.config.js
var config = {
entry: './main.js',
output: {
path:'./',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
module.exports = config;
main.js
import React from 'react';
import ReactDOM from 'react-dom';
import TodoApp from './App.jsx';
import tryThis from './datastructure.js';
ReactDOM.render(<TodoApp />, document.getElementById('app'));
Ok - so my react app is rendering just fine. And I want to add something like this to connect to mongodb:
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
alert("Connected successfully to server");
db.close();
});
but I keep getting errors.. Actually there are 8. I've pasted in the first part. Most of them are to do with the mongodb node module.. I'm so stuck - please help :(
Uncaught Error: Cannot find module "net"
at webpackMissingModule (index.js:38559)
at Object.<anonymous> (index.js:38559)
at Object.<anonymous> (index.js:39140)
at __webpack_require__ (index.js:556)
at fn (index.js:87)
at Object.<anonymous> (index.js:29938)
at __webpack_require__ (index.js:556)
at fn (index.js:87)
at Object.<anonymous> (index.js:29868)
at __webpack_require__ (index.js:556) webpackMissingModule @ index.js:38559 (anonymous) @ index.js:38559 (anonymous) @ index.js:39140
__webpack_require__ @ index.js:556 fn @ index.js:87 (anonymous) @ index.js:29938
__webpack_require__ @ index.js:556 fn @ index.js:87 (anonymous) @ index.js:29868
__webpack_require__ @ index.js:556 fn @ index.js:87 (anonymous) @ index.js:8418
__webpack_require__ @ index.js:556 fn @ index.js:87 (anonymous) @ index.js:588
__webpack_require__ @ index.js:556 (anonymous) @ index.js:579 (anonymous) @ index.js:582 index.js:631 [WDS] Hot Module Replacement enabled. index.js:631 [WDS] Errors while compiling. index.js:669 ./~/mongodb/package.json Module parse failed: C:\myapp\node_modules\mongodb\package.json Unexpected token (2:8) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (2:8)
It seems to me like you're trying to connect to MongoDB from the client side; if that's the case, you should change it to connect to MongoDB from the server. What's on your main.js
file and other files?
I found a basic CRUD sample code using MEAN Stack in github. Hope it helps you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.