Monday 12 June 2017

Node js use variable outside main function and set order of functions

Introduction

I have a three functions, each one would feed data into then next. The objective is first to retrieve data then authenticate a API key then finally using the generated API key and data retrieve from the first function post to the API in the third function.

Order

  1. First function function to retrieve data from a post.

  2. Second function gets API key requested from a API.

  3. Third function posts data to the API.

Needed functionality

I need the variables retried in the first function and the API key generated in the second function to be available for use in the third function.

Problems and questions

  • emailUser is not being found to use in the third function
  • api_key is not being found in the third function
  • also the functions need to run in order first, second then third

This all works if I was to insert the data manual but when input the variables it dose not work, I understand that it is because the variables being within the function but how do I fix this, also how do I set the order of the functions ?

Full code

// Grab the packages needs and sets server
//---------------------------------- Grab the packages we need and set variables --------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------
var express = require('express');
var request = require('request');
var nodePardot = require('node-pardot');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 8080;

// Varibles to use in second and third function
var password = 'password';
var userkey = '6767712';
var emailAdmin = 'admin@admin.com';
// start the server
app.listen(port);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
console.log('Server started! At http://localhost:' + port);

// First Retrieve posted data from Front-End
//---------------------------------- Retrieve posted data from Front-End -----------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------
// POST http://localhost:8080/api/index
app.post('/api/data', function (req, res) {
    console.log(req.body);
    var Fname = req.body.fname;
    var Lname = req.body.lname;
    var emailUser = req.body.email;
    res.send(Fname + ' ' + Lname + ' ' + emailUser);
});

app.get('/', function (req, res) {
    res.send('hello world, Nothing to see here...');
});

// Second Get Posted variables
//---------------------------------- Now authenticate the api and get api_key -----------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------
nodePardot.PardotAPI({
    userKey: userkey,
    email: emailAdmin,
    password: password,
    // turn off when live
    DEBUG: true
}, function (err, client) {
    if (err) {
        // Authentication failed
        // handle error
        console.error("Authentication Failed", err)
    } else {
        // Authentication successful
        // gets api key
        var api_key = client.apiKey;
        console.log("Authentication successful !", api_key);
    }
});

// Third Retrieve posted data from Front-End
//---------------------------------- Send all data to API -----------------------------------------------------
// ------------------------------------------------------------------------------------------------------------
// Set the headers
var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
};

// Configure the request
var options = {
    url: 'https://pi.pardot.com/api/prospect/version/4/do/create/email',
    method: 'POST',
    headers: headers,
    form: {
        'email': emailUser,
        'user_key': userkey,
        'api_key': api_key
    }
};

// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log("error",body)
    }
    else {
        console.log("Sent Data",body);
    }
});



via Beep

No comments:

Post a Comment