简体   繁体   English

解决承诺,直到那时未定义

[英]resolving promise, stil then of undefined

I think im resolving the promise put the code is still saying then of undefined, its not returning the promise, why?我认为我解决了承诺,代码仍然说未定义,它没有返回承诺,为什么?

export const SpacesPutImage = (folder, file, id, data, expertId, avatarUrl) => {

    console.log(folder, file, id, data, expertId, avatarUrl)
    if (avatarUrl === undefined) {
        console.log('si')
        return new Promise(function(resolve, reject) {

            apiCall('put', `/api/experts/${expertId}/spaces`, {
                    data: data,
                    size: file.size,
                    id: id,
                    folder: folder,
                    fileType: file.type
                })
                .then((res) => resolve(res.Location))
                .catch(err => reject(err))


        })
    } else {
        console.log('no')
        return new Promise(function(resolve, reject) {
            apiCall('put', `/api/experts/${expertId}/spaces/${avatarUrl.split("/")[4]}`, {
                    data: data,
                    size: file.size,
                    id: expertId,
                    folder: folder,
                    fileType: file.type
                }).then((res) => resolve(res.Location))
                .catch(err => reject(err))
        })

    }
}

First of all, you don't need new Promise , just return the api call, if it uses .then and .catch it means that it's already a promise.首先,你不需要new Promise ,只是返回的API调用,如果使用了.then.catch这意味着它已经是一个承诺。

export const SpacesPutImage = (folder, file, id, data, expertId, avatarUrl) => {
    console.log(folder, file, id, data, expertId, avatarUrl)
    if (avatarUrl === undefined) {
        console.log('si')
        return apiCall('put', `/api/experts/${expertId}/spaces`, {
            data: data,
            size: file.size,
            id: id,
            folder: folder,
            fileType: file.type
        }).then(res => res.Location)       
    } else {
        console.log('no')
        return apiCall('put', `/api/experts/${expertId}/spaces/${avatarUrl.split("/")[4]}`, {
            data: data,
            size: file.size,
            id: expertId,
            folder: folder,
            fileType: file.type
         }).then(res => res.Location)
    }
}

Please, also add to your question what is apiCall and where do you call SpacesPutImage .请还添加到您的问题什么是apiCall以及您在哪里调用SpacesPutImage

I think im resolving the promise put the code is still saying then of undefined我认为我解决了承诺,代码仍然说未定义

Please provide the correct error and probably where it happens.请提供正确的错误以及可能发生的位置。

One thing that can be happing, as said in the comments is that apiCall doesn't return a promise and saying then of undefined can be about apiCall not returning a promise.正如评论中所说,可能发生的一件事是apiCall不返回承诺, saying then of undefined可能是关于apiCall不返回承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM