2
exports.usersignup= function(req, res) {
console.log('user signed up working')

}
exports.addcustomer= function(req, res) {
console.log('customer added')

}

I have two function which is exports.usersignup and exports.addcustomer .when i call exports.usersignup function it should call exports.addcustomer function and it should work the given logic how can i get this done any one help me out.pls dont give down vote i already tried but its not working

6
  • pass next variable in the usersignup function and when you want to call addcustomer just use return next() statement ; Commented May 24, 2016 at 9:15
  • pls can you give the example code Commented May 24, 2016 at 9:16
  • exports.usersignup= function(req, res,next) { console.log('user signed up working') return next(); } Commented May 24, 2016 at 9:17
  • what is your exact route?? Commented May 24, 2016 at 9:17
  • app.route('/auth/usersignup').post(users.usersignup); Commented May 24, 2016 at 9:18

2 Answers 2

3

Here is a naïve solution:

exports.usersignup = function (req, res) {
  console.log('usersignup')
  exports.addcustomer()
}

exports.addcustomer = function (req, res) {
  console.log('addcustomer')
}

A better solution would be to utilize Express middleware:

// customer.js
// ...

exports.usersignup = function (req, res, next) {
  console.log('usersignup')
  next()
}
exports.addcustomer = function (req, res) {
  console.log('addcustomer')
}

// app.js
// ...

var express = require('express')
var customer = require('./customer.js')
var app = express()
app.post('/user', customer.usersignup, customer.addcustomer)
Sign up to request clarification or add additional context in comments.

5 Comments

this is a better way, but when you are doing db operations like addcustomer then it should be in different .js file which could be imported where ever it is required and used rather than passing next() for saving the user.
i want to pass req.body to customer
i am getting error F:\meanjs\node_modules\aws-sdk\lib\request.js:31 throw err; ^ Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11) at ServerResponse.header (F:\meanjs\node_modules\express\lib\response.js:666:10) at ServerResponse.send (F:\meanjs\node_modules\express\lib\response.js:146:12)
@komal You'll get that error if you call res.json/res.sendStatus or a similar function in both functions.
@komal Please update your question with a SSCCE and I'll try to help you.
0

pass next variable in the usersignup function and when you want to call addcustomer just use return next() statement

6 Comments

pls give full answer
You can use @Alex Booker's answer because me answer is same as he mentioned above
if needed then we can take this conversation in chat
i want to pass req.body to customer function
F:\meanjs\node_modules\aws-sdk\lib\request.js:31 throw err; ^ Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11) at ServerResponse.header (F:\meanjs\node_modules\express\lib\response.js:666:10) at ServerResponse.send (F:\meanjs\node_modules\express\lib\response.js:146:12) i am geting error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.