2017-02-07 111 views
0

我有一個問題搞清楚如何從我的JavaScript文件連接到MongoDB服務。這讓我感到非常沮喪,因爲我一直把頭撞到牆上一個星期,並且找不到解決方案。請幫忙。從Node.js/Javascript連接到MongoDB

我正在運行Node.js和React - 並試圖連接到MongoDB來執行基本的CRUD功能。

的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')); 

好了 - 所以我的反應程序是渲染就好了。我想補充一點這樣的連接到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(); 
}); 

,但我不斷收到錯誤..其實有8我在第一部分粘貼。他們大多是與MongoDB的節點模塊做.. 我如此堅持 - 請幫助:(

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) 

回答

0

像你試圖從客戶端連接到MongoDB的在我看來,如果這樣的話,你應該改變它從服務器連接到MongoDB的。什麼是你的main.js文件和其他文件?