是异步编程的新手,已经阅读了类似的线程,这些线程似乎都有同样的问题,没有返回任何东西,而我的确是......?
但是我仍然收到错误消息“无法读取属性”,然后是“未定义的”
function getWorkItems(iterationPath, projectId) {
var queryClient = VSS_Service.getCollectionClient(TFS_Wit_QueryAPI.WorkItemTrackingHttpClient);
var query = { query: "SELECT [System.Id] FROM WorkItem WHERE [System.IterationPath] = '" + iterationPath + "'" };
var resultOfQuery;
queryClient.queryByWiql(query, projectId).then(
function (resultOfQuery) {
return new Promise((resolve, reject) => {
resolve(resultOfQuery);
console.log("Debug: " + JSON.stringify(resultOfQuery));
})
VSS.notifyLoadSucceeded();
});
}
上面打印调试消息很好,它从服务器获取数据,然后当我在其他地方调用时出现错误
let bar;
getWorkItems(counter.path, projectId).then ( res => {
bar = res;
console.log("Debug: should be output of query " + JSON.stringify(bar));
})
无法读取未定义的属性'然后'
答案 0 :(得分:2)
getWorkItems中没有return语句,所以它隐式返回undefined。此外,由于queryClient.queryByWiql(query, projectId)
已经回复了承诺,因此您不需要创建自己的承诺,并且可以执行以下操作:
function getWorkItems(iterationPath, projectId)
{
var queryClient = VSS_Service.getCollectionClient(TFS_Wit_QueryAPI.WorkItemTrackingHttpClient);
var query = { query: "SELECT [System.Id] FROM WorkItem WHERE [System.IterationPath] = '" + iterationPath + "'" };
return queryClient.queryByWiql(query, projectId);
}
如果您想保留该日志语句并在那里发送notifyLoadSucceeded,您可以,但您仍然不需要创建自己的承诺。如果您处于.then回调中,则只需返回您希望承诺解析的值:
function getWorkItems(iterationPath, projectId)
{
var queryClient = VSS_Service.getCollectionClient(TFS_Wit_QueryAPI.WorkItemTrackingHttpClient);
var query = { query: "SELECT [System.Id] FROM WorkItem WHERE [System.IterationPath] = '" + iterationPath + "'" };
return queryClient.queryByWiql(query, projectId)
.then(function(resultOfQuery) {
console.log("Debug: " + JSON.stringify(resultOfQuery));
VSS.notifyLoadSucceeded();
return resultOfQuery;
});
}