Working with exported functions in Node.js

wuno

Overview

I am incorperating authorize.net payments into my app. Using their sdk they export a function in a way I do not understand. I am looking for some clarity so I can use it in the best way.

I am working with an Angular 2 and node.js app.

Problem

From the Angular2 to side I am making a http post. This post passes form data to the node server. Once I am in the node server I am placing the data into a database. But inside that function I want to pass the data to the authorize.net function which came from the SDK.

Example

Minimal version of my function that I post to.

function 1 :

 updateProfile: function(req, res) {
            var id = req.body.id;
            var email = req.body.email;
            var first_name = req.body.first_name;
            var middle_name = req.body.middle_name;
}

In a different directory called merchant I have two files. One file holds a function to create a user profile in the merchant database. They will hold the cc numbers so I don't have to on my server.

function 2 :

function createCustomerProfile(callback) {
     // Stuff happens in here with the data from the first function
}

I need to pass the data from the first function to the second function. In that same directory they have an index.js file that exports the function like this,

module.exports = {
    createCustomerProfile: require('./create-customer-profile.js').createCustomerProfile
}

Question

With these functions set up like they are. How would I call function 2 from function 1 and pass the data to it? They put callback in the parameters. So I was wondering if this means it has to be called first with function 1 in it as a callback instead of the way I am doing it.

So basically should I be making the http post to function 2. Then require the file with the database insert function in its file? Then apply function 1 as a callback?

Anmol Mittal

Function 1:-

var secondFn = require('path to index.js').createCustomerProfile
updateProfile: function(req, res) {
            var id = req.body.id;
            var email = req.body.email;
            var first_name = req.body.first_name;
            var middle_name = req.body.middle_name;
            secondFn(req.body,function callback(response){
              console.log('customer profile created' , response);
            })

}

Function 2:-

function createCustomerProfile(data,callback) {
     // perform operation
     var response = "custom data"
     callback(response) //call upon completion
}

You can also pass parameters in callback function to check for any error

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related