Node、Express、Mongoose 如何将 doc._id 从函数传递给 next() 中间件
Node, Express, Mongoose how to pass doc._id from a function to next() middleware
路由器:
router.post('/add/someone', ctrl.func1, ctrl.func2);
控制器:
ctrl.func1 = function(req, res, next){
// Save to db
SomeSchemaModel(req.body.data).save(function(err, doc){
if(!err){
// assuming that theres no error
// how can I pass the doc._id
// to the next middleware
// using the next() fucntion?
next();
}
}
}
ctrl.func2 = function(req, res){
// do something with the _id
// from the previous func1
}
因为我从 ctrl.func2
的 req 和 res 中查找了它,但是没有来自之前 ctrl.func1
.
谢谢!
您可以将其附加到您的 req
对象上
ctrl.func1 = function(req, res, next){
// Save to db
SomeSchemaModel(req.body.data).save(function(err, doc){
if(!err){
// assuming that theres no error
// how can I pass the doc._id
// to the next middleware
// using the next() fucntion?
req.docId = doc._id;
next();
}
}
}
ctrl.func2 = function(req, res){
// do something with the _id
// from the previous func1
console.log(req.docId);
}
路由器:
router.post('/add/someone', ctrl.func1, ctrl.func2);
控制器:
ctrl.func1 = function(req, res, next){
// Save to db
SomeSchemaModel(req.body.data).save(function(err, doc){
if(!err){
// assuming that theres no error
// how can I pass the doc._id
// to the next middleware
// using the next() fucntion?
next();
}
}
}
ctrl.func2 = function(req, res){
// do something with the _id
// from the previous func1
}
因为我从 ctrl.func2
的 req 和 res 中查找了它,但是没有来自之前 ctrl.func1
.
谢谢!
您可以将其附加到您的 req
对象上
ctrl.func1 = function(req, res, next){
// Save to db
SomeSchemaModel(req.body.data).save(function(err, doc){
if(!err){
// assuming that theres no error
// how can I pass the doc._id
// to the next middleware
// using the next() fucntion?
req.docId = doc._id;
next();
}
}
}
ctrl.func2 = function(req, res){
// do something with the _id
// from the previous func1
console.log(req.docId);
}