检查API中的Node是否存在电子邮件

时间:2017-06-15 16:18:19

标签: javascript node.js api post xmlhttprequest

简介

好的,我有三个功能。前两个为第三个生成数据。

  1. 获取发布数据(电子邮件)
  2. 获取API密钥
  3. 使用API​​密钥,User_key和电子邮件并将其发布到API
  4. 我需要什么

    我需要第三个将以下内容打印到我的控制台,只要电子邮件存在。

     else {
                        console.log("Login successful !");
                        console.log("API Key", api);
                        console.log("userKey", userkey);
                        console.log("useremail", login_email);
                        console.error("Third You have logged in !");
                    }
    

    我得到了什么

      

    错误null

    即使我发布了一封存在的电子邮件,我也会收到此消息。有人在我的代码中看到我出错的地方吗?

    节点代码

    var firstFunction = function () {
        var promise = new Promise(function (resolve) { // may be redundant
            setTimeout(function () {
                app.post('/test.js', function (req, res) {
                    console.log(req.body);
                    // Get varibles from the post form
                    var login = req.body.LoginEmail;
                    // res.send(email_user);
                    res.send(login);
                    //resolve when get the response
                    resolve({
                        data_login_email: login
                    });
                });
                console.error("First done");
    
            }, 2000);
        });
        return promise;
    };
    
    //---------------------------------- Function to get API key from Pardot (AUTHENTICATION) ------------------------------
    //----------------------------------------------------------------------------------------------------------------------
    var secondFunction = function () {
        var promise = new Promise(function (resolve) {
            setTimeout(function () {
                nodePardot.PardotAPI({
                    userKey: userkey,
                    email: emailAdmin,
                    password: password,
                    DEBUG: false
                }, function (err, client) {
                    if (err) {
                        // Authentication failed
                        console.error("Authentication Failed", err);
                    } else {
                        // Authentication successful
                        var api_key = client.apiKey;
                        console.log("Authentication successful !", api_key);
                        resolve({data_api: api_key});
                    }
                });
                console.error("Second done");
            }, 2000);
        });
        return promise;
    };
    
    //---------------------------------- Function to post data to Pardot ---------------------------------------------------
    // ---------------------------------------------------------------------------------------------------------------------
    function thirdFunction(result) {
        var promise = new Promise(function () {
            setTimeout(function () {
                var headers = {
                    'User-Agent': 'Super Agent/0.0.1',
                    'Content-Type': 'application/x-www-form-urlencoded'
                };
    // Configure the request
                var api = result[1].data_api;
                var userEmail = result[0].data_login_email;
                var options = {
                    url: 'https://pi.pardot.com/api/prospect/version/4/do/read',
                    method: 'POST',
                    headers: headers,
                    form: {
                        'email': userEmail,
                        'user_key': userkey,
                        'api_key': api
                    }
                };
    
    // Start the request
                request(options, function (error, response) {
                    if (!error && response.statusCode == 200) {
                        console.log("error", error);
                    }
                    else {
                        console.log("Login successful !");
                        console.log("API Key", api);
                        console.log("userKey", userkey);
                        console.log("useremail", login_email);
                        console.error("Third You have logged in !");
                    }
                });
            }, 3000);
        });
        return promise;
    }
    
    // sequence of functions
    Promise.all([firstFunction(), secondFunction()])
        .then(thirdFunction);
    

0 个答案:

没有答案