是一个对象,得到 \"5d713a66ec8f2b88b8f830b8\"(字符串类型)""/>
“错误”:“findOneAndUpdate() 的参数 \"filter\" 必须是一个对象,得到 \"5d713a66ec8f2b88b8f830b8\"(字符串类型)"
我正在开发一个关于训练营的 API。我正在使用 nodejs 和 mongodb。
在我的训练营控制器中,我想验证训练营的所有权,这意味着要检查试图验证某个训练营的用户是否实际上是创建训练营的人。
以下是我的更新方法:
exports.updateBootcamp = asyncHandler(async (req, res, next) => {
let bootcamp = await Bootcamp.findById(req.params.id);
if (!bootcamp) {
return next(
new ErrorResponse(`Bootcamp not found with id of ${req.params.id}`, 404)
);
}
// Make sure user is bootcamp owner
if (bootcamp.user.toString() !== req.user.id && req.user.role !== 'admin') {
return next(
new ErrorResponse(
`User ${req.params.id} is not authorized to update this bootcamp`,
401
)
);
}
bootcamp = await Bootcamp.findOneAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true
});
res.status(200).json({ success: true, data: bootcamp });
});
所以我得到一个我认为是正确的字符串,但由于某种原因错误需要一个对象。
这是我的模型训练营。这样你就可以在最后看到 Bootcamp/User 的关系。
const mongoose = require('mongoose');
const slugify = require('slugify');
const geocoder = require('../utils/geocoder');
const BootcampSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please add a name'],
unique: true,
trim: true,
maxLength: [50, 'Name cannot be more than 50 characters']
},
slug: String,
description: {
type: String,
required: [true, 'Please add a description'],
maxLength: [500, 'Description cannot be more than 500 characters']
},
website: {
type: String,
match: [
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/,
'Please use a valid URL with HTTP or HTTPS'
]
},
phone: {
type: String,
maxLength: [20, 'Phone number cannot be longer than 20 characters']
},
email: {
type: String,
match: [
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
'Please add a valid email'
]
},
address: {
type: String,
required: [true, 'Please add an address']
},
location: {
type: {
//GeoJSON Point
type: String,
enum: ['Point']
},
coordinates: {
type: [Number],
index: '2dsphere'
},
formattedAddress: String,
street: String,
city: String,
state: String,
zipcode: String,
country: String
},
careers: {
//Array of strings
type: [String],
required: true,
enum: [
'Web Development',
'Mobile Development',
'UI/UX',
'Data Science',
'Business',
'Other'
]
},
averageRating: {
type: Number,
min: [1, 'Rating must be at least 1'],
max: [10, 'Rating must can not be more than 10']
},
averageCost: Number,
photo: {
type: String,
default: 'no-photo.jpg'
},
housing: {
type: Boolean,
default: false
},
jobAssistance: {
type: Boolean,
default: false
},
jobGuarantee: {
type: Boolean,
default: false
},
acceptGi: {
type: Boolean,
default: false
},
createdAt: {
type: Date,
default: Date.now
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true
}
}, {
toJSON: {virtuals: true},
toObject: {virtuals: true}
});
为什么会这样?这与我的模型或控制器有关吗?
回答如下:更多推荐
“错误”:“findOneAndUpdate() 的参数 \"filter\" 必须是一个对象,得到 \"5d713a66ec8f
发布评论