2016-05-01 1 views
0

だから私はこのテンプレートを持っており、登録フォームから始まるバックエンドを作成しようとしています。私のMongoDBと私のアプリとの間の接続のいくつかの種類があるが、データがデータベースに送信されたありえない、 これはhtmlです:私は、データをポストしようとする試みにしようとしたコードは、ここにユーザーデータをangularjsとノード経由でmongodbに送信しようとしています

<ion-view class="auth-view" cache-view="false"> 
    <ion-nav-bar class="view-navigation"> 
    <ion-nav-back-button> 
    </ion-nav-back-button> 
    </ion-nav-bar> 
    <ion-content class="padding"> 
    <div class="row form-heading"> 
     <div class="col"> 
     <h1 class="form-title">Create your account</h1> 
     <h3 class="form-sub-title">Sign up with Social Network or Email</h3> 
     <div class="social-sign-up button-bar"> 
      <a class="button icon ion-social-facebook button-positive"></a> 
      <a class="button icon ion-social-twitter button-calm"></a> 
     </div> 
     </div> 
    </div> 
    <div class="row form-separator"> 
     <hr class="separator-line"/> 
     <span class="separator-mark">OR</span> 
     <hr class="separator-line"/> 
    </div> 
    <div class="row form-wrapper"> 
     <div class="col"> 
     <form name="signup_form" class="" novalidate> 
      <div class="form-fields-outer list list-inset"> 
      <div class="row multi-inputs"> 
       <div class="col"> 
       <label class="item item-input"> 
        <input class="multi-input" type="text" placeholder="Name" name="name" ng-model="user.name" required> 
       </label> 
       </div> 
       <div class="col"> 
       <label class="item item-input"> 
        <input class="multi-input" type="text" placeholder="Username" name="username" ng-model="user.userName" required> 
       </label> 
       </div> 
      </div> 
      <label class="item item-input"> 
       <input type="email" placeholder="Email" name="email" ng-model="user.email" required> 
      </label> 
      <label class="item item-input"> 
       <input type="tel" placeholder="Phone Number" name="phone" ng-model="user.phone" required> 
      </label> 
      <label class="item item-input" show-hide-container> 
       <input type="password" placeholder="Password" name="password" ng-model="user.password" required show-hide-input> 
      </label> 
      <button type="submit" class="sign-up button button-block" ng-click="doSignUp()" ng-disabled="signup_form.$invalid"> 
       Sign Up 
      </button> 
      <p ng-show="error" class="message error">{{error}}</p> 
      </div> 
     </form> 
     </div> 
    </div> 
    </ion-content> 
</ion-view> 

.controller('CreateAccountCtrl',['$scope', '$http', function($scope, $http){ 
    $scope.doSignUp = function(){ 
     console.log("doing sign up"); 
    $http.post('/accounts', $scope.user).success(function (user) { 
     console.log(user); 
     }); 
//  $state.go('app.feed'); 
    }; 
}]) 

、最終的には私のserver.jsこれまで

var express = require('express'); 
var app = express(); 
var mongojs = require('mongojs'); 
var db = mongojs('accounts',['accounts']); 
var bodyParser = require('body-parser'); 

//app.use(express.bodyParser()); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

app.use(express.static(__dirname + "/www")); 
// app.get('/accounts', function(req, res){ 
// // res.send("i got the request") 

//  db.accounts.find(function(err, docs){ 
//  console.log(docs); 
//  res.json(docs); 
// }); 

// }); 

app.post('/accounts', function(req, res){ 
// console.log(req.body.user); 
    db.accounts.insert(req.body.user, function(err, doc){ 
     res.json(doc); 
    }); 
}); 


app.listen(3000); 
console.log("server running on port 3000"); 

私は間違っていくつかのことがある知っているが、私はちょうど私がまだ勉強として何を知りません。あなたの助け

ための事前のおかげで私はまた、あなたの特急コントローラは、「利用者」と呼ばれ、体内のプロパティを探しているモンゴ

TypeError: Cannot read property '_id' of undefined 
at /Users/beast/node_modules/mongojs/lib/collection.js:83:19 
at /Users/beast/node_modules/mongojs/lib/collection.js:20:7 
at apply (/Users/beast/node_modules/thunky/index.js:16:28) 
at /Users/beast/node_modules/thunky/index.js:20:25 
at /Users/beast/node_modules/mongojs/lib/database.js:36:9 
at /Users/beast/node_modules/mongodb/lib/mongo_client.js:519:11 
at nextTickCallbackWith0Args (node.js:420:9) 
at process._tickCallback (node.js:349:13) 
+0

ここに何かがありますか? - console.log(req.body.user); ' – swapnesh

+0

いいえ、サーバーは即座にクラッシュしますが、.userを削除すると、サインアップをクリックするとidだけを含む空の文字列が作成されます@swapnesh – ORYON100

答えて

0

からこのエラーが発生しました。あなたは、ユーザーオブジェクトを本体にまっすぐに渡しているだけです。あなたの角度コントローラーで次のように試してみてください:

.controller('CreateAccountCtrl',['$scope', '$http', function($scope, $http){ 
    $scope.doSignUp = function(){ 
     console.log("doing sign up"); 
    $http.post('/accounts', { user: $scope.user}).success(function (user) { 
     console.log(user); 
     }); 
//  $state.go('app.feed'); 
    }; 
}]) 
関連する問題