Mongoose request

this is my structure folder
- express_example
| ---- app.js
| ----
| -------- songs.js
| -------- albums.js
| ---- and other expressjs files

song.js

 var mongoose = require('mongoose') , Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var SongSchema = new Schema({ name: {type: String, default: 'songname'} , link: {type: String, default: './data/train.mp3'} , date: {type: Date, default: Date.now()} , position: {type: Number, default: 0} , weekOnChart: {type: Number, default: 0} , listend: {type: Number, default: 0} }); mongoose.model('Song', SongSchema); module.exports = SongSchema; 


album.js

 var mongoose = require('mongoose') , Schema = mongoose.Schema , SongSchema = require('./songs') , ObjectId = Schema.ObjectId; var AlbumSchema = new Schema({ name: {type: String, default: 'songname'} , thumbnail: {type:String, default: './public/images/album/unghoangphuc/U1.jpg'} , date: {type: Date, default: Date.now()} , songs: [SongSchema] }); mongoose.model('Album', AlbumSchema); 


How can I put an album with a code request by album id in the album.js file

+1
source share
1 answer

example:

 var mongoose = require('mongoose') , Album = mongoose.model('Album'); app.get('/posts/:id', function(req, res, next) { Album.findById(req.params.id, function(err, album) { // album is available here }); }); 

see http://mongoosejs.com/docs/finding-documents.html to learn more about finding documents.

PS: this is the third time I answered your question :)

+2
source

Source: https://habr.com/ru/post/1389383/


All Articles