无法从 Promise 获取价值到 Reducer

Can't Get the Value from Promise to Reducer

我是 Promises 和 Async/Await 的新手,我想从 mongoDB 获得一组用户,但是当我得到它时,我收到了它作为承诺,我想在我的减速器中使用它。 我怎样才能做到这一点。 这是我的代码:

import {combineReducers} from 'redux'
import {stitchClient} from '../pages/const'
import {RemoteMongoClient} from 'mongodb-stitch-browser-sdk';
import {DataBase} from '../pages/const';

const mongodb = stitchClient.getServiceClient(
  RemoteMongoClient.factory,
  "mongodb-atlas"
);
const db=mongodb.db(DataBase);
const collection= db.collection('User');

async function fetch(){

    return (
        collection.find().toArray()
        .then(items => {
        console.log(`Successfully found ${items.length} documents.`);
        localStorage.setItem('DataTable',JSON.stringify(items));
        return items;
        })
        .catch(err => console.error(`Failed to find documents: ${err}`))
    )
}

async function GetDataFromFetch(){
    return await fetch();
}

const AllUsersReducer=()=>{
    console.log('My Data',GetDataFromFetch());
    return (GetDataFromFetch())
};

export default combineReducers({
    AllUsers:AllUsersReducer,
});

这是日志: Console Log

您需要在 AllUsersReducer

中使用 async/await
const AllUsersReducer = async () => {
    const fetchData = await GetDataFromFetch()
    console.log('My Data', fetchData);
    return fetchData
}; 

GetDataFromFetch 是异步的,这意味着它的结果将是 Promise。要从 Promise 获得结果,您需要使用 await.