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

2 Answers2

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)
Alex Booker
  • 10,487
  • 1
  • 24
  • 34
  • 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. – Nivesh May 24 '16 at 09:54
  • i want to pass req.body to customer –  May 24 '16 at 10:34
  • 1
    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) –  May 24 '16 at 10:42
  • @komal You'll get that error if you call `res.json`/`res.sendStatus` or a similar function in _both_ functions. – Alex Booker May 25 '16 at 08:19
  • @komal Please update your question with a [SSCCE](http://sscce.org/) and I'll try to help you. – Alex Booker May 25 '16 at 13:12
0

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

Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41
  • pls give full answer –  May 24 '16 at 09:36
  • You can use @Alex Booker's answer because me answer is same as he mentioned above – Atul Agrawal May 24 '16 at 10:06
  • if needed then we can take this conversation in chat – Atul Agrawal May 24 '16 at 10:08
  • i want to pass req.body to customer function –  May 24 '16 at 10:34
  • 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 –  May 24 '16 at 10:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112778/discussion-between-atul-agrawal-and-komal). – Atul Agrawal May 24 '16 at 11:40