I have mongoose schema as given below;
var UserSchema = new Schema({
//...
pets: {type: String, enum: ["Cat", "Dog", "Bird", "Snake"]}
//...
});
// create a new user
const user = new User({
fullname, email, pets:"Cat"
});
try {
await user.save();
res.send({ "success": true, user});
} catch (err) {
res.json({ success: false, message: "Could not add User!", error: err });
}
// to fetch users
const users = await User.find({pets:'Cat'});
res.json({ 'success': true, users });
The response is always empty users array while i have data in db of users having pets value as Cat
{
"success": true,
"users": []
}
Can someone please guide me what's wrong here? or how to get the results with pet as Cat? Thanks in advance.
The problem was with data, i dropped the collection and it started working.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.