从Node.js / Javascript连接到MongoDB

时间:2017-02-07 03:54:10

标签: javascript node.js mongodb

我在查找如何从我的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)

2 个答案:

答案 0 :(得分:1)

我在github中使用 MEAN 堆栈找到了一个基本的CRUD示例代码。希望它可以帮到你。

https://github.com/prabhakarreddy1234/expo-eCode

答案 1 :(得分:0)

在我看来,你正试图从客户端连接到MongoDB;如果是这种情况,您应该将其更改为从服务器连接到MongoDB。您的Imports System.IO Public Class Form1 Private FilesList As List(Of String) Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click Button6.Enabled = False ' Capture list of all files in all subfolders FilesList = Directory.EnumerateFiles(Path.Combine(My.Settings.Path, MetroTextBox1.Text), "*", SearchOption.AllDirectories).ToList ' Initialize ProgressBar MetroProgressBar1.Value = 0 MetroProgressBar1.Maximum = FilesList.Count ' Execute DeleteFiles in separate thread Task.Run(Sub() DeleteFiles()) End Sub Private Sub DeleteFiles() For Each Filename As String In FilesList File.Delete(Filename) UpdateProgress() Next If Me.InvokeRequired Then Me.Invoke(Sub() Button6.Enabled = True) Else Button6.Enabled = True End If End Sub Private Sub UpdateProgress() If Me.InvokeRequired Then Me.Invoke(Sub() UpdateProgress()) Else MetroProgressBar1.Value += 1 MetroProgressBar1.Refresh() End If End Sub End Class 文件和其他文件上有什么内容?