在 Promise 完成之前调用的方法

Method being called before Promise is complete

我正在尝试从 Apache Cordova 应用程序将项目添加到共享点列表。它首先提示用户登录,然后它会生成一个 HTTP Post 来输入数据。

我有以下代码:

function saveToSharepoint(data) {
    var authority = "https://login.microsoftonline.com/common/";
    var authContext = new Microsoft.ADAL.AuthenticationContext(authority);
    var authResult = authContext.acquireTokenAsync("https://my.sharepoint.com", "4be098f8-2184-4831-9ef7-3d17dbbef6a0", "http://localhost:4400/services/office365/redirectTarget.html")
    .then(FormatAndUpload(authResult, data), errorCallback);

}

function FormatAndUpload(authResponse, data) {
    var token = authResponse.accessToken;
    var expiry = authResponse.expiresOn;
    console.log("Token acquired: " + authResponse.accessToken);
    console.log("Token will expire on: " + authResponse.expiresOn);
    $.ajax({
        url: "https://my.sharepoint.com/_api/web/lists/getbytitle('" + Test + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(data),
        headers: {
            "Accept": "application/json;odata=verbose",
            "Authoriztion":"Bearer " + token
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

我遇到的问题是在 acquireTokenAsync 完成之前调用了 FormatAndUpload 方法,因此传递给 FormatAndUpload 方法的 authResponse 变量为空。

我不太熟悉 Javascript/JQuery 中的 promise 框架,但我的印象是事件应该只在前一个方法完成时触发。

有没有人对我在尝试 POST 之前如何正确等待登录完成有任何指示?

你做了什么 FormatAndUpload(authResult, data);是错误的
传递回调的正确方法是

.then(function(authResult){
    FormatAndUpload(authResult, data);
}, errorCallback);

所以你的 saveToSharepoint 会像这样

function saveToSharepoint(data) {
    var authority = "https://login.microsoftonline.com/common/";
    var authContext = new Microsoft.ADAL.AuthenticationContext(authority);
    var authResult = authContext.acquireTokenAsync("https://my.sharepoint.com", "4be098f8-2184-4831-9ef7-3d17dbbef6a0", "http://localhost:4400/services/office365/redirectTarget.html")
    .then(function(authResult){
        FormatAndUpload(authResult, data);
    }, errorCallback);

}

感谢Newbee Dev的回答,你是对的,我没有正确制定then方法。

对于看到此有关 SharePoint 的任何其他人,我实际上重新格式化了代码以使其按预期工作,因此 saveToSharepoint 方法如下所示:

function saveToSharepoint(data) {
var AuthenticationContext = Microsoft.ADAL.AuthenticationContext;
AuthenticationContext.createAsync("https://login.microsoftonline.com/common/")
    .then(function (authContext) {
        authContext.acquireTokenAsync(
            "https://my.sharepoint.com",     // Resource URI
            "4be098f8-2184-4831-9ef7-3d17dbbef6a0",      // Client ID
            "http://localhost:4400/services/office365/redirectTarget.html"    // Redirect URI
        ).then(function (authResult) {
            FormatAndUpload(authResult, data);
        }, function (err) {
            console.log(err);
        });
    }, function (err) {
        console.log(err);
    });
}

要注意的主要事情是异步创建 AuthenticationContext,这样,FormatAndUpload 在整个登录过程完成后调用。只是想我会 post 为其他看到有关 Sharepoint 并被卡住的人。