我正在尝试构建一个Node API,它使用MongoDB从包含以下JSON格式的Book信息的文件中创建和获取信息:
{
Name: String,
Author: String,
Price: Number
}
我无法向数据库添加信息。我得到了Book打印成功的消息。但是当我看到数据库时,只使用ID
和版本密钥_v
创建了一个JSON文档。
以下是我的API代码:
var express = require('express');
var app = express();
var bodyParser=require('body-parser');
var mongoose=require('mongoose');
var book = require('./automobile/reading/book')
//configuring the app for the bosy data intake for post operations
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
var PORT= process.env.PORT || 3000;
mongoose.connect('mongodb://localhost:27017/helloworldapi');
var router = express.Router();
app.use('/api', router);
router.use(function(req,res,next){
console.log('There is something happening in the API');
next();
})
router.route('/books')
.post(function(req,res){
var bookDB = new book();
bookDB.name=req.body.name;
bookDB.author=req.body.author;
bookDB.price=req.body.price;
bookDB.save(function(err){
if(err){
res.send(err);
}
res.json({message: 'Book was successfully printed'});
});
})
.get(function(req,res){
book.find(function(err, books){
if(err){
res.send(err);
}
res.json(books);
});
});
router.route('/books/:book_id')
.get(function(req,res){
book.findById(req.params.book_id, function(err, books){
if(err){
res.send(err);
}
res.json(books)
});
});
router.route('/books/name/:name')
.get(function(req,res){
book.find({name:req.params.name}, function(err, books){
if(err){
res.send(err);
}
res.json(books);
});
});
router.route('/books/author/:author')
.get(function(req,res){
book.find({author:req.params.author}, function(err, books){
if(err){
res.send(err);
}
res.json(books);
});
});
app.listen(PORT);
console.log('server listening on port '+PORT);
在执行GET
操作后尝试执行POST
操作时,我收到以下回复:
[
{
"_id": "5a788cf1ad829e3aa4b91287",
"__v": 0
}
]
以下是我的谢玛:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BookSchema = new Schema({
Name: String,
Author: String,
Price: Number
});
module.exports= mongoose.model('book',BookSchema);
请不要介意根文件夹automobiles
。计划在一开始就创造别的东西。
答案 0 :(得分:2)
您可以使用
router.route('/books')
.post(function(req,res){
var bookDB = new book(req.body);
bookDB.save(function(err){
if(err){
res.send(err);
}
res.json({message: 'Book was successfully printed'});
});
})
但是架构值上的req.body值应该相同。
答案 1 :(得分:1)
我相信这是因为你没有分配到合适的属性。在您的路线中,您可以指定小写属性。但是,Schema定义了大写属性。看看是否匹配案例:
router.route('/books')
.post(function(req,res){
var bookDB = new book();
bookDB.name=req.body.name; // This needs to match the property in the schema exactly
bookDB.author=req.body.author;
bookDB.price=req.body.price;
bookDB.save(function(err){
if(err){
res.send(err);
}
res.json({message: 'Book was successfully printed'});
});
})
var BookSchema = new Schema({
name: String, // Now I'm lower case
author: String,
price: Number
});
或者您可以将路由器设为大写:
router.route('/books')
.post(function(req,res){
var bookDB = new book();
bookDB.Name=req.body.name;
bookDB.Author=req.body.author;
bookDB.Price=req.body.price;
bookDB.save(function(err){
if(err){
res.send(err);
}
res.json({message: 'Book was successfully printed'});
});
})