I'm transforming my code from javascript to typescript, but struggeling with this issue, when trying to add data to the request, which obviously is not possible in typescript. The book, read from MongoDb and added to the request, is used in .get() and .delete() methods.
// bookModel.js
var mongoose = require('mongoose');
var bookModel = mongoose.Schema({
title: String,
author: String,
genre: String,
read:{type:Boolean, default:false}
});
module.exports = mongoose.model('Book', bookModel, 'Book');
// bookController.js
var Book = require('./../models/bookModel'),
module.exports = function(){
var router = express.Router();
router.route('/:bookId')
.all(function(req,res,next){
Book.findById(req.params.bookId,function(err,book){
if(err)
res.status(500).send(err);
else if(book)
{
req.book = book;
next();
}
else
{
res.status(404).send('No book found');
}
})
})
.get(function(req,res){
res.json(req.book);
})
.delete(function(req,res){
req.book.remove(function(err,book){
if(err)
res.status(500).send(err);
else
res.sendStatus(200);
})
})
How can data be added to the request in typescript and how can I retrieve the book in the following methods?