2017-02-07 9 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')); 

[OK]を - ので、私はアプリがうまくレンダリングされる反応。そして、私は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のノードモジュールと関係している - あなたは、クライアント側からの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

助けてください;場合それはケースだ、あなたはmain.jsファイルや他のファイルに何。サーバからのMongoDBに接続するために、それを変更する必要がありますか?