在post请求中发送'data'时输入错误

时间:2017-07-01 21:36:11

标签: javascript node.js express post typeerror

所以我尝试发送一个POST请求,其中包含一个具有“areas”属性的对象。这将返回一些仅位于某个区域的数据。我使用的API是在PHP但我使用的是JavaScript。我已经提出了POST个请求并且已经收到了回复信息,但是当我试图告诉它我想要使用的“区域”时,我无法让它工作。 API说它需要一个区域的数组,但是当我在javascript中执行它时,我得到一个TypeError。这是API:

  

search_area_name:在特定区域内搜索。如果将使用此搜索标准,建议构建一个选择列表以供选择。           值:(数组)

在php中设置数据的一个例子如下:

$data = array (
    'partner_key' => '7e52cad4e91ee36e308d35f93a9db02b', 
    'action' => 'propertySearch',                       
    'return' => 'xml',                                  
    'search_offset' => '0', 
    'search_limit' => '100',                            
    'search_mls_id' => array('1'),
    'search_area_name'=>array("Kimball","Silver Creek Commercial"),
    'debug' => '0'
);

我的POST请求在javascript中设置了我的数据:

app.get("/listing", function (req, res) {

    var listingID      = req.query.id, //passing these in later..
        listingAddress = req.query.address,
        listingArea    = [req.query.area];


    var data = {
        partner_key       : '7e52cad4e91ee36e308d35f93a9db02b',
        action            : 'propertySearch',
        return            : 'json',
        search_offset     : '0',
        search_limit      : '15',
        search_mls_id     : ['1'],
        search_area_name  : listingArea,
        debug             : '0'
    };

    request.post({url: url, formData: data}, function(err, httpResponse, body) {
        if (!err && httpResponse.statusCode == 200) {
            var mlsData = JSON.parse(body);
            res.render('listing', {mlsData: mlsData['data']});
        }
        else {
            return console.error('upload failed:', err);
        }
    });
});

所以我意识到要阅读很多内容,但是如果我取出search_area_name属性,一切都很好,我收到了我期望的确切回复。当我使用search_area_name属性运行脚本时,我收到TypeError。任何建议或意见将不胜感激!

修改,其他信息:

这是我的浏览器将显示的内容(为了以防万一)将其全部放在其中:

TypeError: /Users/blazekotsenburg/WebstormProjects/ResidePC/assets/views/listing.ejs:129
    127| <%include partials/filter-suggest.ejs%>
    128| 
 >> 129| <%include partials/listingGridTemplate.ejs%>
    130| 
    131| <%include partials/footer.ejs%>

esc is not a function
    at rethrow (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:285:18)
    at eval (eval at compile (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:524:12), <anonymous>:268:9)
    at eval (eval at compile (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:524:12), <anonymous>:270:10)
    at returnedFn (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:555:17)
    at tryHandleCache (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:203:34)
    at View.exports.renderFile [as engine] (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/ejs/lib/ejs.js:412:10)
    at View.render (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/express/lib/view.js:128:8)
    at tryRender (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/blazekotsenburg/WebstormProjects/ResidePC/node_modules/express/lib/response.js:971:7)

listingGridTemplate.ejs,即使在没有传递该属性的情况下工作,我认为它失败的地方是这样的:

<div class="row grid-list"><!-- Begin First Row-->
    <div class="container">

        <h1 id="featured-listings">Featured Listings</h1>

        <% mlsData.forEach(function(listing) { %>
            <div class="col-lg-4 col-md-4 col-sm-6">

                <div id= "<%= listing['list_id']%>">

                    <% var listingImage = listing['photos'].split(",");%>
                    <% if (!listingImage[0]) { %>

                        <div class="thumbnail photo-card">

                            <%include listingGridStatus.ejs%>

                            <a href="/listing?id=<%=listing['list_id']%>?area=<%=listing['area_name']%>?address=<%=listing['address']%>">

                            <img class="img-responsive" src="https://source.unsplash.com/wtrAchtpc-w">

                                <p class="no-image-header">No Images <i class="fa fa-frown-o" aria-hidden="true"></i></p>

                                <section class="photo-card-info-spec-row1">
                                    <span class="photo-card-price">
                                        $<%= listing['price'].replace(/\B(?=(\d{3})+(?!\d))/g, ",") %>
                                    </span>
                                    <span class="photo-card-info">
                                        <%include listingGridPropCheck.ejs%>
                                    </span>
                                </section>

                                <section class="photo-card-info-spec-row2">

                                    <span class="photo-card-dom">

                                        <span class="dom">dom:</span>

                                        <%= listing['days_on_market']%>
                                    </span>

                                    <span class="photo-card-punct">|</span>

                                    <span class="photo-card-address">

                                        <%= listing['address']%>,
                                        <%= listing['city']%>,
                                        <%= listing['state']%>
                                    </span>
                                </section>
                            </a>
                        </div>
                    <% } else { %>

                        <div class="thumbnail photo-card">

                            <%include listingGridStatus.ejs%>

                            <a href="/listing?id=<%=listing['list_id']%>&address=<%=listing['address']%>&area=<%=listing['area_name']%>">

                                <img class="img-responsive img-drop-shadow" src="http://www.realcove.net/<%= listingImage[0]%>">

                                <section class="photo-card-info-spec-row1">
                                    <span class="photo-card-price">
                                        $<%= listing['price'].replace(/\B(?=(\d{3})+(?!\d))/g, ",") %>
                                    </span>

                                    <span class="photo-card-info">
                                        <%include listingGridPropCheck.ejs%>
                                    </span>
                                </section>

                                <section class="photo-card-info-spec-row2">

                                <span class="photo-card-dom">

                                    <span class="dom">dom:</span>

                                        <%= listing['days_on_market']%>
                                    </span>

                                    <span class="photo-card-punct">|</span>

                                    <span class="photo-card-address">
                                        <%= listing['address']%>,
                                        <%= listing['city']%>,
                                        <%= listing['state']%>
                                    </span>
                                </section>
                            </a>
                        </div>
                    <% } %>
                </div>
            </div>
        <% }); %>
    </div> 
</div>

2 个答案:

答案 0 :(得分:0)

很抱歉不发布此评论作为评论(还没有50个代表)。我可以给你一个建议,就是你要放一些调试信息,看看究竟是什么传递给你的变量。

console.log([req.query.area]);放在var定义的上方,然后在发布请求之前再添加一个console.log(listingArea)。如果你得到一些乱码值,请检查你的控制台,这应该是你的起点。希望这会帮助你!

编辑: 我相信我找到了一个可以帮助你的相关答案。 试试https://stackoverflow.com/a/15851761/7654934

答案 1 :(得分:0)

我相信你只需要删除req.query.area

周围的方括号
func (me *Str) Start()