Skip to content

multiple where clauses and closure issue #4

Open

Description

@PerterB

Hi,

I have been playing around with the VersionOne JS SDK for node and have a few questions. I based my code on the sample.js file provided with the SDK.

What I'm trying to achieve is a query for Backlog Items where Status.Name is 'Prepared' and Scope.Name is 'My Project'

So I tried this:

v1.query({
    from: "Story",
    where: {
        "Status.Name": "Prepared",
            "Scope.Name": "My Project"
    } // etc.

But that doesn't return any results, if I take out the "Status.Name" or "Scope.Name" property then I get results, I can filter out results based on the property values after they have been returned but it seems it doesn't work when I pass the two values in the 'where' clause. Am I doing something wrong here or is this expected behavior?

Also, I think there may be a defect in the v1meta.js file (or more correctly in the v1meta.coffee file). My 'success' function is being passed the same result object for every invocation. I believe it's down to the following loop in the V1Meta.prototype.query method:

     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        assetxml = _ref[_i];
        oidtoken = assetxml.get('id');
        _ref1 = oidtoken.split(':'), found_type = _ref1[0], found_id = _ref1[1];
        _results.push(_this.get_asset_class(found_type, function(err, Cls) {
          var asset;
          if (err != null) {
            return options.error(err);
          }
          asset = _this.build_asset(Cls, assetxml);
          return options.success(asset);
        }));
      }

The assetxml value is being overwritten everytime the loop iterates, but before the get_asset_class callback is called. Closing over the assetxml variable within the loop resolves it though

        (function(thisAsset) {
            _results.push(_this.get_asset_class(found_type, function(err, Cls) {
              var asset;
              if (err != null) {
                return options.error(err);
              }
              asset = _this.build_asset(Cls, thisAsset);
              return options.success(asset);
            }));
        })(assetxml);

I'm not really familiar enough with coffee script to make any useful contributions however.

I think it would also be useful to pass all the results to the success method rather than each result individually. Or perhaps if all results are passed to a done method instead.

Thanks!

Peter

Activity

  1. JogoShugh commented on Dec 6, 2013

    @JogoShugh
    Contributor

    Peter, thanks for the debugging and ideas! It can be tricky to get closures right in CoffeeScript. We will check that out today or Monday and see if we can get that fixed up.

    Thanks again for the interest in this and submitting such a detailed report.

  2. JogoShugh commented on Dec 6, 2013

    @JogoShugh
    Contributor

    Hey Peter, I'm not sure if you plan to modify data or not. If you only need to query for data, you might be interested in a newer endpoint we created called query.v1:

    http://community.versionone.com/Developers/Developer-Library/Recipes/Tour_of_query.v1
    http://community.versionone.com/Developers/Developer-Library/Documentation/API/Endpoints/query.v1

    Using query.v1 you will get back pure JSON results, and it can accept either JSON or YAML formatted queries.

  3. PerterB commented on Dec 9, 2013

    @PerterB
    Author

    Yes, I do plan on modifying data eventually. I don't think I'll be able to use query.v1 since we're on an older version (12.1 I think), in fact that might be why I can't query using multiple where clauses...

  4. jamesjenner commented on Aug 25, 2014

    @jamesjenner

    Hmm. Just realised that this issue is multiple issues. I stopped reading the first time when I saw the multiple selection criteria problem. So I've created a new issue about the closure problem.

    Is there any update on when this will be resolved?

  5. jamesjenner commented on Aug 26, 2014

    @jamesjenner

    I've found the cause of the multiple where problem. It's in the generated client.js, function V1Server.prototype.get_query_xml.

    The problem is the anonymous function as the input to the push for wherestrs. The function returns an array, however the array.prototype.push() doesn't support an array. I suspect it is converting it to a string which includes the comma delimiter for the string.

    To solve the problem all you need to do is change

     wherestrs.push((function() {
    

    to

    wherestrs = (function() {
    

    Also change the end of the function from:

    })());
    

    to

    })();
    

    Of note is the issue of how to determine the logical operand for each where statement. It forces use of and for example:

    v1.query({
      "from": "Defect",
      "select": [
        'Name',
        'Days',
        'Status.Name',
        'Status.Description',
        'Team.Name',
        'ToDo',
        'Estimate',
        'BlockingIssues.@Sum',
      ],
      "where": {
        "Timebox.Name": 'SprintTwo',
        "Team.Name": 'A-Team',
      },
    

    Is converted to:

    "Timebox.Name=\"SprintTwo\"&Team.Name=\"A-Team\""
    

    To get around this I found a hidden option. If where is not defined then the code is checking for wherestr. It will set the where condition to the value of the wherestr. so in theory the above could be modified to the following:

    v1.query({
      "from": "Defect",
      "select": [
        'Name',
        'Days',
        'Status.Name',
        'Status.Description',
        'Team.Name',
        'ToDo',
        'Estimate',
        'BlockingIssues.@Sum',
      ],
      "wherestr": "Timebox.Name"=SprintTwo&Team.Name='A-Team',
      },
    

    This appears to allow changing the logical condition.

    I hope the above helps anyone coming across this.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions