2017-02-04 13 views
1

nodeJSを使用してmongoDBにデータを挿入しています。nodeJSを使用してmongoDBに "users"コレクションのデータを挿入しません

これは私のserver.jsファイルの一部である:これは私のindex.htmlファイル内の私のコントローラ

app.post('/users', function(req, res, next){ 

db.collection('users', function(err, usersCollection){ 

    usersCollection.insert(req.body, {w : 1} , function(err){ 
     return res.send(); 
    }); 

    }); 
}); 

です:

app.controller('SLController', function ($scope, $http){ 
    $scope.submitSignup = function(){ 
     var newUser = { 
      username: $scope.username, 
      email: $scope.email, 
      password: $scope.password 

     }; 


    $http.post('/users', newUser).then(function(){ 
      alert('Success'); 
     }); 

    } 

    /*$scope.signin = function(){ 
     $http.put('/users/signin', {username: $scope.username, password: $scope.password}) 
      .then(function(){ 
       alert("Successfully signed in."); 
      }, function(err){ 
       alert("Bad login credentials."); 
      }) 
     }*/ 

}); 

が、これは私が「フォームのセクションです私のsl.htmlファイルから挿入しようとしています:

<h3 class="form-titles center-block">Sign Up</h3> 

<input type="text" class="form-control form-subtitles" placeholder="Usuario" ng-model="username"> 

<input type="text" class="form-control form-subtitles" placeholder="E-mail" ng-model="email"> 

<input type="password" class="form-control form-subtitles" placeholder="Password" ng-model="password"> 

<input ng-click="submitSignup()" type="submit" class="form-control form-subtitles btn btn-info" value="Signup"> 

私は他の質問を見ましたが、それらは静かです。

PS。 mongoのコンソールで使用しているコマンドはdb.users.find()です。

ありがとう!

--------------------------------------------- -------------- EDIT ----------------------------------- -----------------------

私はそれを修正するために使用さparcialソリューション:

app.post('/users', function(req, res, next){ 

db.collection('users', function(err, usersCollection){ 

    usersCollection***.save***(req.body, {w : 1} , function(err){ 
     return res.send(); 
    }); 
    }); 
}); 

INSTEAD .insert使用.save

答えて

1

これはシンプルでシンプルなので、簡単にレコーダーを作成できますあなたのdatabase.onlyに2つのパラメータは、電子メールと名前が必要です。

router.all('/create', function (req, res) { 
     var database = req.Collection; 
     var name = req.body.name;// or you can do directly as 'shekhar' 
     var email = req.body.email;// or you can do directly as '[email protected]' 
     var record = new database({ 
      name: name, 
      email: email 
     }); 
     if (record.length > 0) { 
      record.save(function (err, result) { 
       if (err) { 
        res.json({status: 0, message: err}) 
       } else { 
        res.json({status: 1, name: name, email: email, message: " success"}); 
       } 
      }) 
     } else { 
      res.json({status: 0, msg: "Invalid Fields"}); 
     } 
    }); 
+0

私は本当にあなたの方法を理解していません。私はちょうど学び始めて、私はチュートリアルに従っています。私のコードを教えてもらえますか?ありがとう。 –

+0

最初のことは、あなたのAPIを動作させる、私はこれについては角度の必要はないと思う、あなたは単にajaxでデータを送信することができます。 あなたがnodejsで初心者なら、私のgitリポジトリの参照を取得できます。https://github.com/shekhartyagi26/TodoApp/blob/master/routes/index.js –

+0

私はすでにあなたを追加しようとしましたが、私はできません'+'または ' - '記号がありますか? –

0

私はそれを修正するために使用さparcialソリューション:INSTEAD *.insert*使用

app.post('/users', function(req, res, next){ 

    db.collection('users', function(err, usersCollection){ 

    usersCollection***.save***(req.body, {w : 1} , function(err){ 
     return res.send(); 
    }); 
    }); 
}); 

*.save*

関連する問題