Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Node.js从Salesforce获取记录并将其显示在Heroku应用程序上_Node.js_Express_Heroku_Salesforce - Fatal编程技术网

如何使用Node.js从Salesforce获取记录并将其显示在Heroku应用程序上

如何使用Node.js从Salesforce获取记录并将其显示在Heroku应用程序上,node.js,express,heroku,salesforce,Node.js,Express,Heroku,Salesforce,我已经创建了一个Heroku应用程序,可以从Salesforce检索记录(合同),并将其显示在我的Heroku应用程序上。但是我正在努力在node.js代码中编写正确的请求。基本上,当按下“检索合同”时,我希望得到如图所示的合同列表 但在我的例子中,每当我按下“检索合同”{“错误”:“关系”合同“不存在”} 为了从Salesforce获取记录,我使用了“Express.js”和“app.get”(这里是一个片段) 以下是完整的代码: server.js: var express = requ

我已经创建了一个Heroku应用程序,可以从Salesforce检索记录(合同),并将其显示在我的Heroku应用程序上。但是我正在努力在node.js代码中编写正确的请求。基本上,当按下“检索合同”时,我希望得到如图所示的合同列表

但在我的例子中,每当我按下“检索合同”{“错误”:“关系”合同“不存在”}

为了从Salesforce获取记录,我使用了“Express.js”和“app.get”(这里是一个片段)

以下是完整的代码:

server.js:

var express = require('express');
var bodyParser = require('body-parser');
var pg = require('pg');

var app = express();

app.set('port', process.env.PORT || 3000);

app.use(express.static('public'));
app.use(bodyParser.json());

app.post('/update', function(req, res) {
    pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
        // watch for any connect issues
        if (err) console.log(err);
        conn.query(
            'UPDATE salesforce.Contact SET Phone = $1, HomePhone = $1, MobilePhone = $1 WHERE LOWER(FirstName) = LOWER($2) AND LOWER(LastName) = LOWER($3) AND LOWER(Email) = LOWER($4)',
            [req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
            function(err, result) {
                if (err != null || result.rowCount == 0) {
                  conn.query('INSERT INTO salesforce.Contact (Phone, MobilePhone, FirstName, LastName, Email) VALUES ($1, $2, $3, $4, $5)',
                  [req.body.phone.trim(), req.body.phone.trim(), req.body.firstName.trim(), req.body.lastName.trim(), req.body.email.trim()],
                  function(err, result) {
                    done();
                    if (err) {
                        res.status(400).json({error: err.message});
                    }
                    else {
                        // this will still cause jquery to display 'Record updated!'
                        // eventhough it was inserted
                        res.json(result);
                    }
                  });
                }
                else {
                    done();
                    res.json(result);
                }
            }
        );
    });
});

app.get('/getContracts', function(req, res) {

    pg.connect(process.env.DATABASE_URL, function (err, conn, done) {

        // watch for any connect issues
        if (err) {
            console.log(err);
            return;
        }

        conn.query('SELECT Name,Product__c FROM Contract WHERE Product__c != null',
        function(err, result) {
            console.log(result)
            if (err) {
                res.status(400).json({error: err.message});
            }
            else {
                // Need to display 'Success!'
                res.json(result);
            }
        });
        
    });
});

app.listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});
索引html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CRM AXG</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <style>
        body {
            padding-top: 60px;
        }
    </style>
    <script>
        $(function() {
            $("#phoneChangerForm").submit(function(event) {
                event.preventDefault();

                var errorMessage = $("#errorMessage");
                var error = $("#error");
                error.hide();

                $("#message").hide();

                var firstName = $("#firstName").val();
                var lastName = $("#lastName").val();
                var email = $("#email").val();
                var phone = $("#phone").val();

                if (firstName.length == 0 || lastName.length == 0 || email.length == 0 || phone.length == 0) {
                    errorMessage.text("All of the fields are required.");
                    error.show();
                }
                else {
                    $.ajax({
                        url: event.target.action,
                        method: event.target.method,
                        data: JSON.stringify({
                            firstName: firstName,
                            lastName: lastName,
                            email: email,
                            phone: phone
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(data) {
                            $("#firstName").val("");
                            $("#lastName").val("");
                            $("#email").val("");
                            $("#phone").val("");
                            $("#messageMessage").text("Record updated!");
                            $("#message").show();
                        },
                        error: function(err) {
                            errorMessage.text(err.responseJSON.error);
                            error.show();
                        }
                    })
                }
            });
        });

    </script>

    <script>
        $(function() {
            $("#getContractForm").submit(function(event) {
                event.preventDefault();

                var errorMessage = $("#errorMessage");
                var error = $("#error");
                error.hide();

                $("#message").hide();


                $.ajax({
                    url: event.target.action,
                    method: event.target.method,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        $("#name").val("");
                        $("#product__c").val("");
                        $("#messageMessage").text("Success!");
                        $("#message").show();
                    },
                    error: function(err) {
                        errorMessage.text(err.responseJSON.error);
                        error.show();
                    }
                })
                
            });
        });

    </script>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Contact Information</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="phoneChangerForm" action="/update" method="post" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Contact Information</h3>
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="firstName">First Name</label>
                        <input type="text" class="form-control" id="firstName" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="lastName">Last Name</label>
                        <input type="text" class="form-control" id="lastName" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="phone">Phone</label>
                        <input type="tel" class="form-control" id="phone" placeholder="New Phone Number" required>
                    </div>
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>
                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Update Contact</button>
                </div>
            </div>
        </form>
    </div>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Contract Information</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="getContractForm" action="/getContracts" method="get" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Contract List</h3>
                </div>
                <div class="panel-body">
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>

                    <!-- display retrieve contracts result-->
                    <Table>
                        <tbody>
                            <!-- <jS>data.foreach(d) => <tr>d.name</tr></jS> -->
                        </tbody>
                    </Table>

                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Retrieve Contracts</button>
                </div>
            </div>
        </form>
    </div>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">CRM AXG</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="getContractForm" action="/" method="get" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Product List</h3>
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="contractName">Product Name</label>
                        <input type="text" class="form-control" id="contractName" placeholder="For verification" required>
                    </div>
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>
                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Retrieve Products</button>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

CRM AXG
身体{
填充顶部:60px;
}
$(函数(){
$(“#phoneChangerForm”).submit(函数(事件){
event.preventDefault();
var errorMessage=$(“#errorMessage”);
var error=$(“#error”);
错误。隐藏();
$(“#消息”).hide();
var firstName=$(“#firstName”).val();
var lastName=$(“#lastName”).val();
var email=$(“#email”).val();
var phone=$(“#phone”).val();
如果(firstName.length==0 | | | lastName.length==0 | | | email.length==0 | | phone.length==0){
errorMessage.text(“所有字段都是必需的。”);
error.show();
}
否则{
$.ajax({
url:event.target.action,
方法:event.target.method,
数据:JSON.stringify({
名字:名字,
lastName:lastName,
电邮:电邮,,
电话:电话
}),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
$(“#firstName”).val(“”);
$(“#lastName”).val(“”);
$(“#email”).val(“”);
$(“#电话”).val(“”);
$(“#messageMessage”).text(“记录已更新!”);
$(“#消息”).show();
},
错误:函数(err){
errorMessage.text(err.responseJSON.error);
error.show();
}
})
}
});
});
$(函数(){
$(“#getContractForm”).submit(函数(事件){
event.preventDefault();
var errorMessage=$(“#errorMessage”);
var error=$(“#error”);
错误。隐藏();
$(“#消息”).hide();
$.ajax({
url:event.target.action,
方法:event.target.method,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
控制台日志(数据);
$(“#名称”).val(“”);
$(“产品c”).val(“”);
$(“#messageMessage”).text(“Success!”);
$(“#消息”).show();
},
错误:函数(err){
errorMessage.text(err.responseJSON.error);
error.show();
}
})
});
});
联系方式
名字
姓
电子邮件
电话
错误:
更新联系人
合同清单
错误:
检索合同
产品清单
品名
错误:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CRM AXG</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <style>
        body {
            padding-top: 60px;
        }
    </style>
    <script>
        $(function() {
            $("#phoneChangerForm").submit(function(event) {
                event.preventDefault();

                var errorMessage = $("#errorMessage");
                var error = $("#error");
                error.hide();

                $("#message").hide();

                var firstName = $("#firstName").val();
                var lastName = $("#lastName").val();
                var email = $("#email").val();
                var phone = $("#phone").val();

                if (firstName.length == 0 || lastName.length == 0 || email.length == 0 || phone.length == 0) {
                    errorMessage.text("All of the fields are required.");
                    error.show();
                }
                else {
                    $.ajax({
                        url: event.target.action,
                        method: event.target.method,
                        data: JSON.stringify({
                            firstName: firstName,
                            lastName: lastName,
                            email: email,
                            phone: phone
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(data) {
                            $("#firstName").val("");
                            $("#lastName").val("");
                            $("#email").val("");
                            $("#phone").val("");
                            $("#messageMessage").text("Record updated!");
                            $("#message").show();
                        },
                        error: function(err) {
                            errorMessage.text(err.responseJSON.error);
                            error.show();
                        }
                    })
                }
            });
        });

    </script>

    <script>
        $(function() {
            $("#getContractForm").submit(function(event) {
                event.preventDefault();

                var errorMessage = $("#errorMessage");
                var error = $("#error");
                error.hide();

                $("#message").hide();


                $.ajax({
                    url: event.target.action,
                    method: event.target.method,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        $("#name").val("");
                        $("#product__c").val("");
                        $("#messageMessage").text("Success!");
                        $("#message").show();
                    },
                    error: function(err) {
                        errorMessage.text(err.responseJSON.error);
                        error.show();
                    }
                })
                
            });
        });

    </script>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Contact Information</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="phoneChangerForm" action="/update" method="post" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Contact Information</h3>
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="firstName">First Name</label>
                        <input type="text" class="form-control" id="firstName" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="lastName">Last Name</label>
                        <input type="text" class="form-control" id="lastName" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" placeholder="For verification" required>
                    </div>
                    <div class="form-group">
                        <label for="phone">Phone</label>
                        <input type="tel" class="form-control" id="phone" placeholder="New Phone Number" required>
                    </div>
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>
                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Update Contact</button>
                </div>
            </div>
        </form>
    </div>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Contract Information</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="getContractForm" action="/getContracts" method="get" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Contract List</h3>
                </div>
                <div class="panel-body">
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>

                    <!-- display retrieve contracts result-->
                    <Table>
                        <tbody>
                            <!-- <jS>data.foreach(d) => <tr>d.name</tr></jS> -->
                        </tbody>
                    </Table>

                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Retrieve Contracts</button>
                </div>
            </div>
        </form>
    </div>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">CRM AXG</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <form id="getContractForm" action="/" method="get" style="width: 400px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Product List</h3>
                </div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="contractName">Product Name</label>
                        <input type="text" class="form-control" id="contractName" placeholder="For verification" required>
                    </div>
                </div>
                <div class="panel-footer">
                    <div id="message" class="alert alert-info" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span id="messageMessage"></span>
                    </div>
                    <div id="error" class="alert alert-danger" role="alert" style="display: none;">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                        <span class="sr-only">Error:</span>
                        <span id="errorMessage"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Retrieve Products</button>
                </div>
            </div>
        </form>
    </div>
</body>
</html>
conn.query('SELECT Name,Product__c FROM salesforce.Contract WHERE Product__c != null'