最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何解决'我有作者和书本模式,在邮递员结果中,我想为一本书显示两个或更多作者'

IT培训 admin 5浏览 0评论

如何解决'我有作者和书本模式,在邮递员结果中,我想为一本书显示两个或更多作者'

我想要这样的邮递员结果

[{
 title:
 year:
 rating:
 authors: [{
    name: 
    birthday:
    country: 
  }]
}]

我想要两个或更多作者,但我只有一位作者

 model/book.js

  const mongoose = require('mongoose');
  const authorSchema = require('../models/author');


 const bookSchema = new mongoose.Schema({
   title:{
       type:String,
       required:true,
       min: 5,
       max: 50
   },
   rating:{
       type: Number,
       required: true,
       min:0,
       max:10
   },
   authors: {
       type: authorSchema,
       required: true
   },

 });
   const Book= new mongoose.model('Book', bookSchema);



route/book.js

router.get('/', async(req, res)=>{
    const books= await Book
    .find({}, { _id:0, __v:0 })
     res.send(books);
});  

router.post('/', async(req, res)=>{
const author = await Author.findById  (req.body.authorId);
if(!author) return res.status(400).send('Invalid Author');

let book= new Book({
    title: req.body.title,
    rating: req.body.rating,
    authors:[{
        name: author.name,
        birthday: author.birthday,
        country: author.country
    }]
});

book= await book.save();
res.send(book)

});
module.exports =router;

我通过邮递员的POST方法输入此{“ title”:“学习Python”,“ rating”:“ 9”,“ authorId”:[“ 5d99ac95f17917117068631b”,“ 5d99ad75c4edd61f98af740b”]}

然后我得到只有第一作者,作者数组不显示

回答如下:

[findById通过提供的_id字段查找单个文档

使用find和$in来匹配带有_id数组的文档数。

[Author.find({ $in: req.body.authorId })将为您提供与req.body.authorId匹配的作者列表。

然后通过遍历find实例的new Book查询的结果来创建作者数组。

如何解决'我有作者和书本模式,在邮递员结果中,我想为一本书显示两个或更多作者'

我想要这样的邮递员结果

[{
 title:
 year:
 rating:
 authors: [{
    name: 
    birthday:
    country: 
  }]
}]

我想要两个或更多作者,但我只有一位作者

 model/book.js

  const mongoose = require('mongoose');
  const authorSchema = require('../models/author');


 const bookSchema = new mongoose.Schema({
   title:{
       type:String,
       required:true,
       min: 5,
       max: 50
   },
   rating:{
       type: Number,
       required: true,
       min:0,
       max:10
   },
   authors: {
       type: authorSchema,
       required: true
   },

 });
   const Book= new mongoose.model('Book', bookSchema);



route/book.js

router.get('/', async(req, res)=>{
    const books= await Book
    .find({}, { _id:0, __v:0 })
     res.send(books);
});  

router.post('/', async(req, res)=>{
const author = await Author.findById  (req.body.authorId);
if(!author) return res.status(400).send('Invalid Author');

let book= new Book({
    title: req.body.title,
    rating: req.body.rating,
    authors:[{
        name: author.name,
        birthday: author.birthday,
        country: author.country
    }]
});

book= await book.save();
res.send(book)

});
module.exports =router;

我通过邮递员的POST方法输入此{“ title”:“学习Python”,“ rating”:“ 9”,“ authorId”:[“ 5d99ac95f17917117068631b”,“ 5d99ad75c4edd61f98af740b”]}

然后我得到只有第一作者,作者数组不显示

回答如下:

[findById通过提供的_id字段查找单个文档

使用find和$in来匹配带有_id数组的文档数。

[Author.find({ $in: req.body.authorId })将为您提供与req.body.authorId匹配的作者列表。

然后通过遍历find实例的new Book查询的结果来创建作者数组。

发布评论

评论列表 (0)

  1. 暂无评论