{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
0
Question by Brinda Marfatia · Jan 23, 2018 at 04:39 PM · 110k Views node.jsnode

How to get the return value of one node.js method in another nodejs script in Apigee?

I have 2 node.js scripts in Apigee Node.js ABC and Node.js DEF. Now ABC is the main script and DEF is an object script. I have a method DEFMethod in DEF which returns the status code. I am trying to call that DEFMethod in ABC and DEFMethod return a value which should be passed to ABC. After ABC gets that value it proceeds accordingly. But I am having issue in passing the value. The check is always undefined.

This is how I call DEFMEthod it in ABC script

ds =new DEF(req, resp);
check = ds.DEFMethod(select_contact,resp);

DEF script

function(error, response, body){if(response.statusCode ==200){//go back 
                console.log("Response:",body);
                console.log("Status Code:",response.statusCode);return resp.sendStatus(200);}else{
                console.log("Response:",body);
                console.log("Status Code:",response.statusCode);return resp.sendStatus(404);}});

Please let me know what the issue is?

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

3 Answers

  • Sort: 
avatar image
1
Best Answer

Answer by Dino   · Jan 25, 2018 at 05:54 PM

I see, I understand now. Yours is a common question with JavaScript.

In nodejs, there is a common pattern, called "Asynchronous callbacks". Or "Asynchronous Javascript". There is a good overview here. Basically the idea is: invoke a function and DON'T examine the immediate return code of that function. Instead, give that function, ANOTHER function to call (passing a status code or other return value) when the first function finishes. The SECOND function is how you get the result of the first function.

This is really common in nodejs modules, and it is the pattern used by the kvm.get() function. Look at it: you pass a key, and a function. The latter is known as a "callback function". The immediate return value of the kvm.get() function is expected to be ignored - the only way information comes out of it, is via the callback function. The request() function you are using, to get a URL... works the same way.

Your DirectorySearch.prototype.callDirectorySearch function wraps both the kvm.get() and the request(). To get what you want, the callDirectorySearch() must ALSO accept a callback function, and you'll use it in the same way you use the cb in the kvm.get or the request().

The code looks like this:

DirectorySearch.prototype.callDirectorySearch = function(select_contact, resp, cb) {
  var kvm = access.getKeyValueMap("Authorization", 'environment');
  kvm.get('ID', function(err, key_value) {
    // this is the anonymous cb function for kvm.get()
    request( {
        url : 'URL', 
        headers : {
          "apikey" : key_value
        }
      },
      function(error, response, body) {
        // this is the anon cb fn for request()
        console.log("Response:", body);
        console.log("Status Code:", response.statusCode);
        // now, invoke the cb fn that was passed to callDirectorySearch
        cb(response.statusCode); 
      });
  });
};

And to invoke it, you would have something like this in your main script:

var DirectorySearch = require('./CallDirectorySearch.js');
var ds = new DirectorySearch(req, resp);
ds.callDirectorySearch(select_contact, resp, function(statusCode) {
  console.log('status code received: ' + statusCode);
});

There may be other optimizations to make in your code. For example I don't see any use of the select_contact or the resp parameters in the callDirectorySearch() function. Why pass them? Also, I don't see any use of this.requestNamespace or this.responseNamespace either. So why have DirectorySearch be a class ? But maybe you are not finished implementing the code yet.

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brinda Marfatia · Jan 25, 2018 at 08:06 PM 0
Link

It worked. Thank you!!

avatar image
0

Answer by Dino   · Jan 23, 2018 at 07:42 PM

Just pointing it out for other readers:

This is a basic nodejs question. Has nothing to do with Apigee's hosting of nodejs.

Here's what you do.

First, your main module. like this

var express = require('express'),
    app = express(),
    port,
    helper = require('./helper.js');


//app.set('json spaces', 2);


function unhandledRequest(req, response, next){
  response.status(400)
    .json({ error: "unhandled request"})
    .end();
}


function requestHandler(request, response, next) {
  var outboundPayload = {
        message: "hello",
        time: (new Date()).toISOString(),
        value: helper.f1()
      };
  response.status(200)
    .json(outboundPayload)
    .end();
}


app.get('/*', requestHandler);


app.use(unhandledRequest);


port = process.env.PORT || 5950;
app.listen(port, function() {
  console.log('Echo Listening on ' + port);
});

Now the helper module. What you're calling "DEF". like this:

function myFunction1(req, resp) {
  return 5;
}


function myFunction2(req, resp) {
  return new Date ();
}


module.exports = {
  f1: myFunction1,
  f2: myFunction2
};

Then, the main module can call functions exported by the helper module.

See the line that calls helper.f1() .

If you want to pass arguments to those functions, go right ahead. They're just functions.

good luck!

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brinda Marfatia · Jan 25, 2018 at 05:14 PM 0
Link

Hi Dino,

I am able to call the function from helper module in the main script. But that helper module after processing, returns a status code and I want that status code to be passed to main script. I am having issue with that.

This is what I am doing in main script:

var DirectorySearch = require('./CallDirectorySearch.js');
ds = new DirectorySearch(req, resp); 
check = DirectorySearch.callDirectorySearch(select_contact, resp);

I want check to have the status code so that I can compare the value and process further.

This is my DirectorySearch script

(function() {
    'use strict';
    var http = require('http');
    var request = require('request');
    var access = require('apigee-access');
    var statuscode;
    console.log('Calling DirectorySearch to check the netid');

    function DirectorySearch(requestNamespace, responseNamespace) 
    {
        this.requestNamespace = requestNamespace;
        this.responseNamespace = responseNamespace;
    }

    DirectorySearch.prototype.callDirectorySearch = function(select_contact,resp)
    {
        var kvm = access.getKeyValueMap("Authorization", 'environment');
        kvm.get('ID', function(err, key_value)
        {
            request(
            {
                url : 'URL', 
                headers : 
                {
                    "apikey" : key_value
                }
            },
            function(error, response, body) 
            {
                if (response.statusCode == 200) 
                {
                    //go back 
                    statuscode = 200;
                    console.log("Response:", body);
                    console.log("Status Code:", response.statusCode);
                    return statuscode;
                } 
                else 
                {
                    statuscode = 404;
                    console.log("Response:", body);
                    console.log("Status Code:", response.statusCode);
                    return statuscode;
                }
            });
        });
    };
    module.exports =  {
      callDirectorySearch: callDirectorySearch
    };
}());
avatar image
0

Answer by Galvani Makau Futa · Jan 14, 2019 at 04:00 PM

Hi @Dino,

I got the same problem, I try to make a RETURN but it won't work..

Can you help me?

'use strict';
var express = require('express');
var router = express.Router();

/* GET home page. */

//Function 2: createTag
var createTag = function hi (TanentValue) {
    var https = require('https');

    var data = JSON.stringify({
        name: TanentValue,
        schemaPath: "Tag"
    });
    var options = {
        hostname: 'qlik_dev.mcs.be',
        path: '/meteor/qrs/tag?xrfkey=1234567890123456',
        method: 'POST',
        headers: {
            'x-qlik-xrfkey': '1234567890123456',
            'hdr-usr': 'MCS\\gaka',
            'Content-Type': 'application/json'
        },
    };

    var req = https.request(options, (res) => {
        //console.log(res)

        res.on('data', (d) => {
            console.log("hi tag")
            var getResult = "GaLvAnI"; // ----> return this and use it into the function createStream
            return getResult;
        })
    })
        ;
    req.on('error', (error) => {
        console.error(error)

    });

    req.write(data);
    req.end();
}
//Function 1: createStream
var createStream = function (TanentValue) {
    var https = require('https');
    var galvani = hi(); // --------> here I made a variable to call return value
    var data = JSON.stringify({
        name: TanentValue,
    });

    var options = {
        hostname: 'qlik_dev.mcs.be',
        path: '/meteor/qrs/stream?xrfkey=1234567890123456',
        method: 'POST',
        headers: {
            'x-qlik-xrfkey': '1234567890123456',
            'hdr-usr': 'MCS\\gaka',
            'Content-Type': 'application/json'
        },
    };

    var req = https.request(options, (res) => {

        res.on('data', (d) => {
            console.log(galvani); // -----> use the variable here
        })
    })
        ;
    req.on('error', (error) => {
        console.error(error)
    });

    req.write(data);
    req.end();
}
//homepage
router.get('/', function (req, res) {
    res.render('index', { title: 'MCS Test' });
});
//create
router.post('/create', function (req, res) {
    //create tag
    console.log('POST / Call Create Tag');
    createTag(req.body.TanentValue);
    //create stream
    console.log('POST / Call Create Stream');
    createStream(req.body.TanentValue);

    res.send('Stream and Tag has been created');
});
module.exports = router;

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dino-at-Google ♦♦   · Jan 14, 2019 at 04:13 PM 0
Link

Hi

I don't clearly understand the problem you're having.

Again it seems to be a nodejs problem. It's possible the forums for Apigee Edge are not the best place to get help for your problem. Did you try reading about callbacks?

If yours is an Apigee-related question, please do post your question as a new question, and I'll try to answer there.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sporadic/Intermittent issues with node js 1 Answer

node script error during high load conditions 1 Answer

Hosted targets Apigee - How to retrieve query params in index.js 1 Answer

Is there some way to set a timeout for NodeJS backend server ? 1 Answer

how to stream an api response body to client 9 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges