如何将表单中的数据保存到Mongodb数据库?

时间:2021-11-30 07:37:44

as a project I'm trying to convert my todo application to save the todos in a mongdb database instead of a json file. So I set up the backend and changed my functions in angular to make requests to the backend but now I've got a few problems.

作为一个项目,我正在尝试转换我的待办事项应用程序,以保存在mongdb数据库而不是json文件中的待办事项。所以我设置后端并改变我的角度函数来向后端发出请求,但现在我遇到了一些问题。

  1. The boxes where the todos are posted are empty, even though I specified a name value in the database for them
  2. 发布todos的框是空的,即使我在数据库中为它们指定了名称值

  3. Whenever I load the page I get the data from the database. As a test I also print what I get to the console. Right now I'm getting five empty todo boxes while in the database I have to objects. I can also see that I have two objects from the console.
  4. 每当我加载页面时,我都会从数据库中获取数据。作为测试,我还打印了我到控制台的内容。现在我得到五个空的待办事箱,而在数据库中,我有对象。我还可以看到控制台中有两个对象。

  5. When I do a post request using postman I specify the name of the todo task but in the database the task only has the parameters that are automatically generated from the mongodb database.
  6. 当我使用postman执行发布请求时,我指定了todo任务的名称,但在数据库中,任务只有从mongodb数据库自动生成的参数。

Here is my server.js file:

这是我的server.js文件:

//connect to database
mongoose.connect('mongodb://localhost:27017/myTodoApp');

// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public'));

app.use(bodyParser.json());

//define our model for the todos
var Todo = mongoose.model('Todo', {
  name: String,
});

//when I get a get request I'm going to send
//the index.html file
app.get('/', function(req, res){
  res.sendFile( __dirname + '/public/index.html');
});

//get all the todos from the database
app.get('/api/todos', function(req, res){
  Todo.find(function(err, todos){
    if(err)
      res.send(err)
    res.json(todos);
  });
});

//create a todo
app.post('/api/todos', function(req,res){
  Todo.create({
    name: req.body.text,
    checked: false
  }, function(err, todo){
      if(err)
        res.send(err);
      Todo.find(function(err, todos){
        if(err)
          res.send(err)
        res.json(todos);
      });
  });
});

app.delete('/api/todos/:todo_id', function(req, res){
  Todo.remove({
    _id: req.params.todo_id
  }, function(err, todo){
       if(err)
        res.send(err);
      Todo.find(function(err, todos){
        if(err)
          res.send(err);
        res.json(todos);
      });
  });
});

app.listen(3000);

And here is my controller from which I connect the frontend to the backend:

这是我的控制器,我将前端连接到后端:

var app = angular.module('myApp', ['navigationDirective']);

app.controller('MainController', ['$scope','$http', function($scope, $http){
  $scope.formData = {};
  //return the data from the json file
  $scope.loadData = function(){
    $http.get('/api/todos')
      .then(function(data){
        $scope.todos = data;
        console.log(data);
      })
    };

    //call the loadData function that returns the data from the json file
    $scope.loadData();


    $scope.addTodo = function() {
    $http.post('/api/todos', $scope.formData)
        .success(function(data) {
            $scope.formData = {}; // clear the form so our user is ready to enter another
            $scope.todos = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
    };

    /*$scope.addTodo = function(){
      $scope.todos.push({name: $scope.newTodo, checked: false});
      $scope.updateData({name: $scope.newTodo, checked: false});
      $scope.newTodo = '';
      console.log('Works');
    };*/

}]);

Any ideas of what I'm doing wrong?

我做错了什么想法?

I uploaded the main files that I'm using for this problem on plunkr

我在plunkr上传了我正在使用的主要文件

1 个解决方案

#1


0  

If you want to save more fields, you need to define them in your Todo Model Schema. If the field isn't defined, Mongoose won't save it.

如果要保存更多字段,则需要在Todo模型模式中定义它们。如果未定义该字段,Mongoose将不会保存它。

var Todo = mongoose.model('Todo', {
    name: String,
    checked: Boolean,
    date: {
        type: Date,
        default: Date.now
    }
});

#1


0  

If you want to save more fields, you need to define them in your Todo Model Schema. If the field isn't defined, Mongoose won't save it.

如果要保存更多字段,则需要在Todo模型模式中定义它们。如果未定义该字段,Mongoose将不会保存它。

var Todo = mongoose.model('Todo', {
    name: String,
    checked: Boolean,
    date: {
        type: Date,
        default: Date.now
    }
});