Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

hey everyone so i have this template and i am trying to create a backend for it starting with the registration form. although there is some type of connection between my mongodb and my app, data isnt being sent to the database, this is the html:

    <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>

here is the code i tried in attempt to post the data

.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');
    };
}])

and finally my server.js so far

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");

i know there is a few things wrong but i just dont know what as i am still learning. thanks in advance for your help

i have also encountered this error from mongo

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)
share|improve this question
    
Are you getting something here ? - console.log(req.body.user); – swapnesh May 1 '16 at 18:41
    
no, the server crashes instantly but if i remove the .user, when i click sign up, it creates empty strings containing just an id @swapnesh – ORYON100 May 1 '16 at 19:50

Your express controller is looking for a property in the body called "user". Your are just passing the user object straight into the body. In your angular controller try the following:

.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');
    };
}])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.