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

I'm trying to save data in mongoDB from angular. My data became from angular page to node.js page and from their I want to save the data in mongoDB. I sent the data I want to save to the controller. and my schema cams from localhost:8080/activities thanks,

my angular look like that:

 <html ng-app="addActivityApp">
     <body ng-controller="activityController">
     <h1 class="text-center">Add Activity</h1>
            <form class="form-group" ng-submit="createActivity()">
              <label for="inputEmail3" class="col-sm-2 control-label">Title:</label>
                <input class="form-control" type="text" placeholder="Title" ng-model="title"></input>
                <label for="inputEmail3" class="col-sm-2 control-label">Age:</label>
                <input class="form-control" id="inputEmail3" type="number" placeholder="Age" ng-model="age" min="0" max="16"></input>
                <label for="inputEmail3" class="col-sm-2 control-label">Description:</label>
                 <textarea class="form-control" id="inputEmail3" type="text" placeholder="Description" ng-model="description"></textarea>
                 <br>
                 <label for="inputEmail3" class="col-sm-2 control-label">Themes:</label>
                <input type="submit" class="btn btn-default" name="send"></input>
            </form> 
        <script src="js/lib/angular/angular.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.js"></script>
        <script src="js/addController.js"></script>
     </body>
     </html>

and my controller looks like that:

var addActivityApp = angular.module('addActivityApp',[]);

var model = {};

addActivityApp.controller('activityController',function($scope) {
    $scope.addActivityApp = model;
});

addActivityApp.run(function($http) {
    $http.get("http://localhost:3000/activities").success(function(data){
addActivityApp.createActivity= function($scope){
    var activity = new Activity();
    activity.title = $scope.title;
    activity.age = $scope.age;
    activity.description = $scope.description;
    activity.theme = $scope.theme;
    activity.$save(function (result) {
      $scope.activities.push(result);
      $scope.title = '';
      $scope.age = '';
      $scope.description = '';
      $scope.theme = '';
            });
        }
    });
});

I read about it and I can't understand what I did wrong

localhost:3000/activities

var mongoose = require('mongoose');
var Activity = require('./activity');

exports.getData = function(req, res){
    Activity.find({},function(err, docs){
        console.log("docs "+docs);
        res.json(docs);
        return;
    });
}
share|improve this question
    
Add your nodejs code here – Abdul Rehman Sayed Jun 9 '16 at 9:48
    
of my schema? or my get to the page? – user3488862 Jun 9 '16 at 11:07
    
this one localhost:3000/activities – Abdul Rehman Sayed Jun 10 '16 at 3:37

Not 100% sure about angularjs, but there is a nodejs module that you could include in your project for mongodb https://www.npmjs.com/package/mongodb.

Using it is as simple as:

var MongoClient = require('mongodb').MongoClient;
var assert      = require('assert');

/**
 * save array of json objects to mongodb
 * @param issues
 */
Mongo.saveToMongo = function (issues) {

    // Use connect method to connect to the Server
    MongoClient.connect(mongourl, function (err, db) {
        assert.equal(null, err);
        console.log("Connected successfully to Mongo");

        // Get the issues collection
        var collection = db.collection('issues');

        console.log("Inserting issues");

        // insert all issues retrieved
        collection.insertMany(issues, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted new issues");
            db.close();
        });
    });
}

At the very least you could use it to retro fit your own angular connection.

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.