Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const userSchema = new mongoose.Schema({
- username: { type: String },
- email: { type: String },
- isVerified: { type: Boolean, default: false },
- picVersion: { type: String, default: '1531305955' },
- picId: { type: String, default: 'default.png' },
- images:{
- [
- {
- imgId: { type: String, default: '' },
- imgVersion: { type: String, default: '' }
- }
- ],
- validate: [arrayLimit, 'You can upload only 4 images']
- }
- city: { type: String, default: '' },
- });
- function arrayLimit(val) {
- return val.length <= 4;
- }
- UploadImage(req, res) {
- cloudinary.uploader.upload(req.body.image, async result => {
- await User.update(
- {
- _id: req.user._id
- },
- {
- $push: {
- images: {
- imgId: result.public_id,
- imgVersion: result.version
- }
- }
- }
- )
- .then(() =>
- res
- .status(HttpStatus.OK)
- .json({ message: 'Image uploaded successfully' })
- )
- .catch(err =>
- res
- .status(HttpStatus.INTERNAL_SERVER_ERROR)
- .json({ message: 'Error uploading image' })
- ...
- const userSchema = new mongoose.Schema({
- username: { type: String },
- email: { type: String },
- isVerified: { type: Boolean, default: false },
- picVersion: { type: String, default: '1531305955' },
- picId: { type: String, default: 'default.png' },
- images: {
- type:[{
- imgId: { type: String, default: '' },
- imgVersion: { type: String, default: '' }
- }],
- validate: [arrayLimit, 'You can upload only 4 images']
- },
- city: { type: String, default: '' },
- });
- function arrayLimit(val) {
- return val.length <= 4;
- }
- let user = await User.findById(req.user._id);
- user.images.push({
- imgId: result.public_id,
- imgVersion: result.version
- });
- await user.save();
Add Comment
Please, Sign In to add comment