私はtodoアプリケーションを実行しています。私のコードでは、ng-repeat
ディレクティブを使用して、データベースにあるように多くのtodoboxを作成しています。しかし、私の場合であってもデータベースが空であっても、5つのToDoボックスがあれば。それはおそらく私が呼んでいる私のloadData
の機能と関係がありますが、私はそれを理解することはできません。 Here is an image of what I get on my browser combined with the data from the debugger.。 localhost:3000/api/todosでリクエストを取得すると、データベースが空であるため、[]
が正しく表示されます。何か案は?ここで空のmongodbデータベースからデータを取得するのはなぜですか?
<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);
ありがとう、それは解決策です。私の心はまだ.successメソッドを使っていました... – captain