1

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?

5
  • i didn't fully understand, but it looks like you have the control over function 2, so why not pass another argument into it? function createCustomerProfile(data, callback) {...}. Commented Dec 13, 2016 at 9:42
  • well, callback is a general way to call a function, it really depends fully on what they do with that callback, and what arguments they pass into it, so without the code where they use the callback parameter i can't tell you what they wanted. Commented Dec 13, 2016 at 9:45
  • response from the server? so the function do some calls, and return the data from their server in the callback as parameter? can you give a link to the api function? Commented Dec 13, 2016 at 9:49
  • well, in nodejs if you require a folder, it will look for "index.js" file, and will require it as you require the file specifically, so if foo is folder require('./foo') == require('./foo/index.js') == require('./foo/index'). Commented Dec 13, 2016 at 9:52
  • Let us continue this discussion in chat. Commented Dec 13, 2016 at 9:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes so if I do, firstFunction() { var data; secondFn.createCustomerProfile(data) { //callback response in here } // how do I use callback from second function in here FirstFunction }
check edited ans , when you call callback in second function , the callback function passed will be called , i.e function callback in the first function

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.