如何使用AngularJS和mongoDB添加新对象

时间:2015-12-02 16:16:42

标签: angularjs node.js mongodb mongoose

我正在使用带有mongoose的NodeJS,ANgularJS和MongoDB来建立一个网站。我在mongoDB中添加对象时遇到了一些麻烦。对象的名称是 Todo

以下是Todo的模型:

var mongoose = require('mongoose');
var TodoSchema = new mongoose.Schema({
    name: String,
    password : String,
    completed: Boolean,
    note: String  
});

module.exports = mongoose.model('Todo', TodoSchema);

在控制器中我创建了一个新的Todo并将其推送

angular.module('app').controller('Connexion', ['$scope', 'Todos','$location', function($scope,Todos, $location) {

$scope.editing = [];
$scope.todos = Todos.query();

$scope.save = function() {
    var todo = new Todos({ name: "test", password: "test", completed: false });
        $scope.todos.push(todo);
        Todos.save($scope.todo);
}
}]);

这是我的html页面,每次点击按钮我都会创建一个新的待办事项并显示在屏幕上:

   <button ng-click="save()">Creer POST </button>

    <ul>
        <li ng-repeat="todo in todos">
            {{todo.name}}
            {{todo.password}}

       </li>
   </ul>

但是我遇到了问题,新的对象没有添加到数据库中。我该怎么办?

这是我在后端的文件:

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var Todo = require('../models/Todo.js');

/* GET /todos listing. */
router.get('/', function(req, res, next) {
   Todo.find(function (err, todos) {
      if (err) return next(err);
           res.json(todos);
   });
});

/* POST /todos */
router.post('/', function(req, res, next) {
   Todo.create(req.body, function (err, post) {
      if (err) return next(err);
        res.json(post);
   });
});

/* GET /todos/id */
router.get('/:id', function(req, res, next) {
   Todo.findById(req.params.id, function (err, post) {
       if (err) return next(err);
          res.json(post);
    });
});


/* PUT /todos/:id */
router.put('/:id', function(req, res, next) {
   Todo.findByIdAndUpdate(req.params.id, req.body, function (err, post) {
       if (err) return next(err);
          res.json(post);
   });
});

/* DELETE /todos/:id */
router.delete('/:id', function(req, res, next) {
    Todo.findByIdAndRemove(req.params.id, req.body, function (err, post) {
     if (err) return next(err);
       res.json(post);
  });
});

module.exports = router;

如果这可以帮助这个后端文件app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var mongo = require('mongodb');
var Post = require('./models/Post.js');

var routes = require('./routes/index');

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/todoApp', function(err) {
   if(err) {
      console.log('connection error', err);
   } else {
      console.log('connection successful');
   }
});


var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', function(req, res, next){
res.sendFile(__dirname + '/public/index.html');
});

var found = ['DB Connection not yet established.  Try again later.   Check the console output for error messages if this persists.'];


module.exports = app;

1 个答案:

答案 0 :(得分:0)

来自$scope.todo

Todos.save($scope.todo);未定义,因此您可能无法保存任何内容