[英]mongoose router find by reference properety
我正在我的项目中工作,我已经为后端编写了 server.js 文件,这是我的 mongoose 架构和 model
const BookSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
isbn: {
type: String,
required: true
},
author: {
type: String,
//required: true
},
description: {
type: String
},
published_date: {
type: Date
},
publisher: {
type: String
},
updated_date: {
type: Date,
default: Date.now
}
});
// make data model from schema
const Book = mongoose.model('book', BookSchema);
这是我的路由器
router.get('/:id', (req, res) => {
Book.findById(req.params.id)
.then(book => res.json(book.title))
.catch(err => res.status(404).json({ nobookfound: 'No Book found' }));
});
如何通过获取请求传递 Book.isbn 以获取 Book.title
只需使用 findOne 方法
Book.findOne({isbn:"pass isbn value here"})
.then(book => res.json(book.title))
.catch(err => res.status(404).json({ nobookfound: 'No Book found' }));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.