Skip to content

Instantly share code, notes, and snippets.

/1imageModel.js Secret
Created May 19, 2017

Embed
What would you like to do?
import mongoose from 'mongoose';
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const imageSchema = new Schema({
title: String,
categories: [{type: Schema.Types.ObjectId, ref: 'Category'}],
description: String,
path: String,
createdAt: {type: Date, default:Date.now},
isDeleted: {type: Boolean, default: false}
});
const Image = mongoose.model('Image', imageSchema);
export default Image;
import mongoose from 'mongoose';
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const categorySchema = new Schema({
name: {type:String, ref:'Image'},
createdAt: {type:Date, default: Date.now},
isDeleted: {type: Boolean, default: false}
});
const Category = mongoose.model('Category', categorySchema);
export default Category;
imageController.getAll = (req,res)=>{
db.Image.find({}).populate('categories')
.then( images =>{
res.json(images)
})
};
[
{
"_id": "590787970a477629a4dd45d3",
"title": "Bra Girl",
"description": "Girl portrait",
"path": "http://res.cloudinary.com/amazecpk/image/upload/v1493665693/acu1vuajlg1rklzfqduk.png",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-05-01T19:08:07.441Z",
"categories": [
{
"_id": "58fc3d9fa5ed6233486cc977",
"name": "Personal",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-04-23T05:37:35.352Z"
},
{
"_id": "58fc3dc0a5ed6233486cc979",
"name": "Portraits",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-04-23T05:38:08.734Z"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.