0
私は最初の平均スタックアプリを構築しています。thisスコッチ記事。クマが正しく作成されない
POST
リクエストを私のlocalhost
に送っている時点で、Postman経由で新しいクマ(モデル)を作成しています。
[
{"_id":"589f6ecc734d1d56393c9444","name":"Klaux"},
{"_id":"589f6f924af04a03d8870dea","__v":0}
]
た:
問題は、私はすべてのクマを参照する要求を送信するときに、私は次のよう取り戻すということです新しいdocument
。私はポストマンを経て作成要求を送信する際に上述したように、それが正しく(配列の2番目のオブジェクトを。)が作成されていない
ここで要求を送信するコードです:
// /api/bears
router.route('/bears')
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
console.log('Bears',bears);
res.json(bears);
});
})
.post(function(req, res) {
var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
// save the bear and check for errors
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear created!' });
});
});
app.use('/api', router);
とデータベースのコレクション次のようになります。
はなぜレコードが正しく作成されていませんか?EDIT
クマのモデルは次のようになります。これは、実際に記事の下部にあるコメント欄に答え頼まれた
var mongoose = require('mongoose');
var BearSchema = new mongoose.Schema({
name: String
});
module.exports = mongoose.model('Bear', BearSchema);
あなたのクマのモデルはどのようなものですか? –
@LuisDiegoHernández私の編集を見る – Katana24