【问题标题】:Unable to use createQuery() in typescript无法在打字稿中使用 createQuery()
【发布时间】:2018-08-02 01:48:12
【问题描述】:

我正在尝试使用以下内容创建查询 - https://www.visualstudio.com/en-us/docs/integrate/extensions/reference/client/api/tfs/workitemtracking/restclient/workitemtrackinghttpclient2_2#method_createQuery

我正在使用上述开发一个 vsts 扩展。这是代码-

import { QueryHierarchyItem  } from "TFS/WorkItemTracking/Contracts";
var postedQuery = [

    {
        "children": [],
        "clauses": {
            "field": {
                "referenceName": "System.WorkItemType",
                "name": "Work Item Type",
                "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/System.WorkItemType"
            },
            "operator": {
                "referenceName": "SupportedOperations.Equals",
                "name": "="
            },
            "value": "Bug"
        },

        "columns": [
            {
                "referenceName": "System.Id",
                "name": "ID",
                "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/System.Id"
            },
            {
                "referenceName": "System.Title",
                "name": "Title",
                "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/System.Title"
            },
            {
                "referenceName": "System.State",
                "name": "State",
                "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/System.State"
            }
        ],
        "createdBy": {
            "id": "d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
            "displayName": "Jamal Hartnett <fabrikamfiber4@hotmail.com>"
        },
        "createdDate": "2016-06 - 01T16: 58:56.64Z",
        "filterOptions": "WorkItems",
        "hasChildren": false,
        "id": "df60fdf6-3b5f-4928-aae8-29ee63df6e31",
        "isDeleted": false,
        "isFolder": false,
        "isInvalidSyntax": true,
        "isPublic": false,
        "lastModifiedBy": {
            "id": "d291b0c4-a05c-4ea6-8df1-4b41d5f39eff",
            "displayName": "Jamal Hartnett <fabrikamfiber4@hotmail.com>"
        },
        "lastModifiedDate": "2016-06 - 01T16: 58:56.64Z",
        "name": "All Bugs",
        "path": "Shared Queries",
        "queryType": "flat",
        "sortColumns": [
            {
                "field": {
                    "referenceName": "Microsoft.VSTS.Common.Priority",
                    "name": "Priority",
                    "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/Microsoft.VSTS.Common.Priority"
                },
                "descending": false
            },
            {
                "field": {
                    "referenceName": "System.CreatedDate",
                    "name": "Created Date",
                    "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields/System.CreatedDate"
                },
                "descending": true
            }
        ],

        "wiql": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Bug' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc",


    }

]



 let queryPath = "Shared Queries";
    let Query: QueryHierarchyItem = postedQuery;

   client.createQuery(Query, "Team_P1", queryPath).then((wi) => {


    },
        (query) => {


        });

【问题讨论】:

    标签: typescript azure-devops hierarchy children createquery


    【解决方案1】:

    首先,你可以参考西里尔的回答。

    其次,你可以参考这段代码:

    let Query:any={
                name:"Api Query",
                wiql: "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Bug' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
            };
    let queryPath = "Shared Queries";
    
    
       client.createQuery(Query, "Team_P1", queryPath).then((wi) => {
    
    
        },
            (query) => {
    
    
            });
    

    【讨论】:

      【解决方案2】:

      默认情况下,tsc 编译器会将错误消息截断为 100 个字符。

      您可以通过在 tsconfig.json 文件的 compilerOptions 属性中设置 "noErrorTruncation": true 来更改它。

      详情请见Typescript Compiler Options

      错误信息是现在(美化后)

      '类型' { "children": any[]; "clauses": { "field": { "referenceName": string; "name": string; "url": string; }; "operator": { "referenceName": string; "name": string; }; "value": string; }; "columns": { "referenceName": string; "name": string; "url": string; }[]; "createdBy": { "id": string; "displayName": string; }; "createdDate": string; "filterOptions": string; "hasChildren": boolean; "id": string; "isDeleted": boolean; "isFolder": boolean; "isInvalidSyntax": boolean; "isPublic": boolean; "lastModifiedBy": { "id": string; "displayName": string; }; "lastModifiedDate": string; "name": string; "path": string; "queryType": string; "sortColumns": { "field": { "referenceName": string; "name": string; "url": string; }; "descending": boolean; }[]; "wiql": string; }[] ' 不可分配给类型 'QueryHierarchyItem'。 类型 [重复类型] 中缺少属性“children”。 '

      您将 postedQuery 声明为数组 []QueryHierarchyItem 这就是缺少属性 children 的原因。

      如果您删除该数组,您将收到缺少属性等的新错误消息。

      顺便说一句,基于this link,看起来您不需要创建整个对象,您可以创建一个空对象并分配所需的参数。

      let queryPath = "Shared Queries";
      let query: <QueryHierarchyItem>{};
      query.Name = 'Query Name'; 
      query.wiql = '...'
      
      client.createQuery(query, "Team_P1", queryPath)
            .then(wi => {
                console.log(wi);             
            }, q => {
                console.log(q);
            });
      

      【讨论】:

      猜你喜欢
      • 2019-01-21
      • 2023-02-17
      • 2023-03-26
      • 2019-05-10
      • 2018-10-21
      • 1970-01-01
      • 2021-08-31
      • 2019-01-23
      • 2021-09-30
      相关资源
      最近更新 更多