gpt4 book ai didi

JavaScript 异步返回 'then not defined'

转载 作者:行者123 更新时间:2023-12-01 02:10:19 24 4
gpt4 key购买 nike

我是异步编程的新手,读过类似的线程,这些线程似乎都具有不返回任何内容的相同问题,而我的却......?

但是我仍然收到错误消息“无法读取未定义的属性‘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 + "'" };
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));
})

无法读取未定义的属性“then”

最佳答案

getWorkItems 中没有 return 语句,因此它隐式返回 undefined。此外,由于 queryClient.queryByWiql(query, projectId) 已经返回 Promise,因此您无需创建自己的 Promise,并且可以执行以下操作:

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 在那里,您可以,但您仍然不需要创建自己的 promise 。如果您处于 .then 回调中,则只需返回您希望 Promise 解析为的值:

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

关于JavaScript 异步返回 'then not defined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49800792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com