FirebaseError:函数DocumentReference.set()用无效数据调用。不支持的字段值:自定义Promise对象

时间:2018-12-24 13:08:30

标签: javascript reactjs firebase react-native google-cloud-firestore

我有一个涉及承诺的问题。

我运行writeNewPost()时收到以下错误消息:

  

FirebaseError:函数DocumentReference.set()用无效数据调用。不支持的字段值:自定义的Promise对象
  FirebaseError:函数DocumentReference.set()用无效数据调用。不支持的字段值:自定义的Promise对象
      在新的FirestoreError(blob

该如何解决?这是我的代码:

export const shrinkImageAsync = async (uri) => {
    return ImageManipulator.manipulateAsync(uri, [{resize: {width: 400}}], {});
};

function urlToBlob(url) {
    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();
        xhr.onerror = reject;
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                resolve(xhr.response);
            }
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob'; // convert type
        xhr.send();
    })
}

export const uploadPhoto = (uri, path) => {
    return new Promise(async (res, rej) => {
        const blob = await urlToBlob(uri);
        const ref = firebase.storage().ref(path);
        const unsubscribe = ref.put(blob).on(
            'state_changed',
            state => {
            },
            err => {
                unsubscribe();
                rej(err);
            },
            async () => {
                unsubscribe();
                const url = await ref.getDownloadURL();
                res(url);
            },
        );
    });
};

export const uploadPhotoAsync = async (imageAndDescription) => {
    const path = `post/can/${uuid.v4()}.jpg`;
    const {uri: reducedImage} = await shrinkImageAsync(
        imageAndDescription.image,
    );
    const imgUrl = await uploadPhoto(reducedImage, path);
    let newImageAndDescription = {
        'description': imageAndDescription.description,
        'imageUrl': imgUrl
    };
    return newImageAndDescription;
};

export const writeNewPost = () => {
    return async (dispatch, getState, {getFirebase, getFirestore}) => {
        const firestore = getFirestore();
        let newPostRef = firestore.collection('posts').doc();
        dispatch({type: POST_ID_CHANGED, payload: newPostRef.id});
        const {postDescription, postTitle, selectedGroupName, postType, postImagesAndDescription} = getState().postDescription;
        const {uid, displayName} = getState().firebase.auth;
        let newPost = {
            authorId: uid,
            author: displayName,
            description: postDescription,
            title: postTitle,
        };
        let newPostImagesAndDescription = await _.chain(postImagesAndDescription).map(uploadPhotoAsync).value();
        newPost['imagesAndDescription'] = newPostImagesAndDescription;
        await newPostRef.set(newPost);
    }
};

writeNewPost()

Object Screenshot

0 个答案:

没有答案