Skip to content

Double callback when using multiple custom post types? #84

Closed

Description

@PanMan

Hi!
If I do this
wp.posts().type(['cityreport','place']).get(function (err, data) {
I get a console error "double callback", and then a "syntax error". Also my error callback gets called.
The URL called does include the right data, but parsing it fails? It is valid JSON.
Or am I doing something wrong?

Activity

  1. PanMan commented on Nov 14, 2014

    @PanMan
    ContributorAuthor

    I did some more debugging, and if I copy the content for the requested URL to a static page, the library doesn't crash. So I suspect it has something to do with headers, or encodings.
    The error I get is: double callback! ERR [SyntaxError: Unexpected token <]
    Any ideas how to fix this? Not being able to request multiple post types is really frustrating, especially since it is in one of the example uses.

  2. kadamwhite commented on Nov 14, 2014

    @kadamwhite
    Collaborator

    Hey @PanManAms, sorry I missed this issue when you opened it last week! Off the top of my head I'm not sure what might be going on, but I'll try to look into it this weekend. Multiple types is definitely something we want to support.

  3. PanMan commented on Nov 14, 2014

    @PanMan
    ContributorAuthor

    Thanks for looking into it, it's kind of critical for us. The strange part is that the URL's get made correctly, its in parsing the result that it breaks. Let me know if an example url helps (and how I can privately send it). I did test it with wp.root (and wp.url) and both have the same behaviour. Thnx!

  4. kadamwhite commented on Nov 14, 2014

    @kadamwhite
    Collaborator

    An example URI would help; let me know how you'd best prefer to send it my way.

  5. PanMan commented on Nov 14, 2014

    @PanMan
    ContributorAuthor

    if you follow me back on twitter (@PanMan ) I'll DM it. I couldn't find your email on your website?

  6. self-assigned this
    on Nov 18, 2014
  7. kadamwhite commented on Nov 18, 2014

    @kadamwhite
    Collaborator

    While I am not yet able to reproduce the issue described (double callback!), I am seeing an issue where superagent is transforming the (already-formatted) type[]=type1&type[]=type2 portion of the URL into type[][0]=type1&type[][1]=type2. This results in the following error:

    http://[test site url]/wp-json/posts?filter%5Bcity%5D=cityname&type%5B%5D=event&type%5B%5D=evttype
    { [Error: cannot GET /wp-json/posts?filter%5Bcity%5D=cityname&type%5B%5D[0]=event&type%5B%5D[1]=evttype (403)]
      status: 403,
      method: 'GET',
      path: '/wp-json/posts?filter%5Bcity%5D=cityname&type%5B%5D[0]=event&type%5B%5D[1]=evttype' }
    

    This can certainly be avoided by removing the [] around the type property, since superagent evidently inserts the array syntax and indices itself; however, we will also be reevaluating whether we're using the right request library, since this is not intuitive behavior.

  8. PanMan commented on Nov 21, 2014

    @PanMan
    ContributorAuthor

    Btw, for now I have worked around it by using request: Building the data with wordpress-rest-api and then requesting the url (and returning the data) with request. Might be helpful for others. Still should work tho :)

  9. modified the milestone: 0.3.1 on Dec 3, 2014
  10. kadamwhite commented on Dec 4, 2014

    @kadamwhite
    Collaborator

    @PanManAms this should be resolved by 0.3.1, released this morning: try updating the package and let me know if the issue is still occurring!

  11. PanMan commented on Dec 4, 2014

    @PanMan
    ContributorAuthor

    HI. Unfortunately this is still occurring. I just did an NPM update, but unfortunately still get the same double callback.
    I'm not sure this is directly related to #89.
    I have been able to work around this, by using this library to build the url, and calling _renderURI() and requesting that url with the 'request' package.
    I can't reopen the issue here, but at my end it is still broken.
    Regards,
    PanMan.

  12. PanMan commented on Dec 4, 2014

    @PanMan
    ContributorAuthor

    Hi.
    I created this minimal test case, which at my end still crashes:

    var WP = require('wordpress-rest-api');
    wp = new WP({
        endpoint: "http://dev.admin.stedenintransitie.nl/wp-json",
        auth: false 
    });
    var toget=wp.posts().type(['fieldtrip','event']);
    //var toget=wp.url('http://mysite.nl/wp-json/posts?filter%5Bcity%5D=amsterdam&type%5B%5D=event&type%5B%5D=fieldtrip');
    toget.get(function (err, data) { 
        console.log(err,data);
        if (err) {
            console.log('ERR');
            return;
        }
        console.log(data);
    }); 
    

    Output I get is:

    double callback!
    [SyntaxError: Unexpected token <] undefined
    ERR
    

    I tested on 2 different machines here, both crash.

  13. kadamwhite commented on Dec 4, 2014

    @kadamwhite
    Collaborator

    @PanManAms The issue will still occur if you use the wp.url() method, because the array indices will not be present in the string; e.g., wp.url('yoururl/wp-json/posts?type[]=type1&type[]=type2') will fail, but wp.url('yoururl/wp-json/posts?type[0]=type1&type[1]=type2') should succeed. Additionally, any URL created with the URL builder methods such as posts() should succeed.

  14. PanMan commented on Dec 4, 2014

    @PanMan
    ContributorAuthor

    Hmmm, you are right, it IS fixed now...

  15. kadamwhite commented on Dec 4, 2014

    @kadamwhite
    Collaborator

    Glad to hear it! Let me know if you find any other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions