(MEAN STACK) 后端正在保存 objectId 和 __v : 0 in mongodb 当我提交表单时

(MEAN STACK) backend is saving objectId and __v : 0 in mongodb when i submit a form

我正在使用 MEAN STACK 开发一个小型 CRUD 应用程序:

后端:

booksRoute.route('/post').post((req,res,next)=>{
    var x = {
        name   : req.params.name,
        genre  : req.params.genre,   
        author : req.params.author,
        rating : req.params.rating,
        price  : req.params.price
    }
    console.log(x) ; 
    BookModel(x).save((err,x)=>{
        if (err){console.log(err);}
        else res.send(x) ;
    }) ;
})

猫鼬模型

const mongoose = require('mongoose') ;
const Schema = mongoose.Schema ; 

let books = new Schema({
    name:{
        type:String
    },
    genre:{
        type:String
    },
    author:{
        type:String
    },
    rating:{
        type:Number
    },
    price:{
        type:Number
    }
},{
    collection: 'booklist' 
}) 

module.exports = mongoose.model('books',books) ; 

前端

<ul>
    <form #f="ngForm" (ngSubmit)="onSubmit(f)">  
    <li>
    <label>book's name *</label>
    <input name="name" type="text" ngModel>
    </li>
    <li>
    <label>book's genre *</label>
    <input name="genre" type="text" ngModel>
    </li>
    <li>
    <label>book's author *</label>
    <input name="author" type="text" ngModel>
    </li>
    <li>
    <label>rating *</label>
    <input name="rating" type="number" [max]="10" min="0" ngModel>
    </li>
    <li>
    <label>price *</label>
    <input name="price" type="number" ngModel>
    <input class="btn btn-primary" type="submit"/> 
    </li>  
</form>
</ul>

触发后台保存文档的函数

 onSubmit(f){
    //console.log(f.value) ;
    this.apiService.postBook(f.value).subscribe( res => {console.log('the back response:',res)}) ; 
  }

服务

//post book
  postBook(data){
    console.log(data) ;
    return this.http.post(this.backendUrl+'/post',data) ;
  }

我认为你的问题是你试图从 req.params 而不是 req.body

中获取值