Javascript 为什么我要从一个空的mongodb数据库中获取数据?
我正在做一个todo应用程序,在我的代码中,我使用Javascript 为什么我要从一个空的mongodb数据库中获取数据?,javascript,angularjs,mongodb,Javascript,Angularjs,Mongodb,我正在做一个todo应用程序,在我的代码中,我使用ng repeat指令创建尽可能多的todo数据库。但在我的情况下,即使数据库是空的,如果得到五个待办事项框。它可能与我调用的loadData函数有关,但我无法理解。当我在localhost:3000/api/todos上执行get请求时,我得到了[],这是正确的,因为数据库是空的。有什么想法吗 这是我的html文件的一部分: <div class="row"> <h1>Things you have to do<
ng repeat
指令创建尽可能多的todo数据库。但在我的情况下,即使数据库是空的,如果得到五个待办事项框。它可能与我调用的loadData
函数有关,但我无法理解。当我在localhost:3000/api/todos上执行get请求时,我得到了[]
,这是正确的,因为数据库是空的。有什么想法吗
这是我的html
文件的一部分:
<div class="row">
<h1>Things you have to do</h1>
<li ng-repeat="todo in todos">
<p id="todobox"> {{ todo.name }} </p>
</li>
<form>
<input type="text" placeholder="I need to do..." ng-model="formData.newTodo" required>
<button ng-click="addTodo(formData)">Add</button>
</form>
</div>
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to database
mongoose.connect('mongodb://localhost:27017/tododbtest');
// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public'));
//app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//define our model for the todos
var Todo = mongoose.model('Todo', {
name: String,
//maybe change it to bool later
//checked: boolean
});
//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)
console.log(todos);
res.json(todos);
});
});
//create a todo
app.post('/api/todos', function(req, res){
console.log("Req.body.name:" + req.body.name);
Todo.create({name: req.body.name, checked: false},
function(err, todo){
if(err) res.send(err);
Todo.find(function(err, todos){
if(err) res.send(err);
res.json(todos);
});
});
});
app.listen(3000);
这是我的server.js
文件:
<div class="row">
<h1>Things you have to do</h1>
<li ng-repeat="todo in todos">
<p id="todobox"> {{ todo.name }} </p>
</li>
<form>
<input type="text" placeholder="I need to do..." ng-model="formData.newTodo" required>
<button ng-click="addTodo(formData)">Add</button>
</form>
</div>
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to database
mongoose.connect('mongodb://localhost:27017/tododbtest');
// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public'));
//app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//define our model for the todos
var Todo = mongoose.model('Todo', {
name: String,
//maybe change it to bool later
//checked: boolean
});
//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)
console.log(todos);
res.json(todos);
});
});
//create a todo
app.post('/api/todos', function(req, res){
console.log("Req.body.name:" + req.body.name);
Todo.create({name: req.body.name, checked: false},
function(err, todo){
if(err) res.send(err);
Todo.find(function(err, todos){
if(err) res.send(err);
res.json(todos);
});
});
});
app.listen(3000);
$http
服务返回解析为响应对象的承诺<代码>数据是响应对象的五个属性之一。将todos
设置为response.data
$http.post('/api/todos', $scope.formData)
.then(function(response){
console.log($scope.formData.name);
$scope.todos = response.data;
$scope.formData = {};
});
$http
服务返回解析为响应对象的承诺<代码>数据是响应对象的五个属性之一。将todos
设置为response.data
$http.post('/api/todos', $scope.formData)
.then(function(response){
console.log($scope.formData.name);
$scope.todos = response.data;
$scope.formData = {};
});