如何解决通过参数传递objectId时,无法从MongoDB获取所有文档详细信息
我只得到文档中存在的对象...
这是路线的代码行
router.get('/product-by-id/:ProductId',async (req,res) => {
const {ProductId} = req.params;
const Id= await Prod.findById({_id:ProductId})
return res.send(Id);
})
const mongoose= require('mongoose');
const Schema= mongoose.Schema;
const productSchema = mongoose.Schema({
author: {
type: Schema.Types.ObjectId,ref: 'users'
},title: {
type:String,maxlength: 50
},description:{
type: String
},file: {
data: Buffer,contentType: String
},duration:{
type:String
},cat: {
type:String,enum:["Shoes","Appliances","Apparel","Accessories","Electronics","Books","PC Parts"]
},barter:{
type: String
}
},{timestamp: true })
const Prod = mongoose.model('Prod',productSchema);
module.exports= Prod
我在Postman上测试了路由,但仅获取文件对象,而不获取其他对象... 非常感谢您的帮助。
解决方法
您的方法看起来不错,在使用findbyId直接传递ID时,只需要进行一点修复即可。像这样
let ProductId = req.params.ProductId
const Id= await Prod.findById(ProductId)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。