简体   繁体   中英

Type error when sending 'data' in post request

So I am trying to send a POST request with an object that has a property for "areas". This will return me some bit of data that is located only around some area. The API I am using is in php but I'm using javascript. I have already made POST requests and have been getting information back, but I can't get it to work when I try to tell it what "area" I want to use. The API says that it takes an array for the area, but when I do it in javascript, I'm getting a TypeError . Here is the API:

search_area_name: Search within specific areas. Recommend to build a pick list to choose from if this search critieria will be used. values: (array)

An example of setting up the data in php is as so:

$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'
);

My POST request in javascript with my data set up:

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);
        }
    });
});

So I realize that it is a lot to read, but if I take out the search_area_name property, all is well and I receive the exact response I expect. When I run the script with the search_area_name property, I receive TypeError . Any advice or input would be greatly appreciated!

Edit, Additional Info:

This is what my browser will display (put it all in there just in case):

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)

and the listingGridTemplate.ejs , which is where I guess it is failing even though it works when that property is not passed, is as so:

<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>

Sorry for not posting this as a comment (do not have 50 rep yet). A suggestion I could give you is for you to put some debugging messages to see what exactly gets passed to your variables.

put a console.log([req.query.area]); just above your var definitions, and put one more console.log(listingArea) just before your post request. Check your console if you are getting some gibberish values and that should be your starting point. Hope this helps you out!

EDIT: I believe I found a relevant answer that might help you out. Try https://stackoverflow.com/a/15851761/7654934

我相信您只需要删除req.query.area周围的方括号即可

listingArea    = req.query.area;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM