reactjs - Wordpress Gutenberg Block - ReactJS - 从 Firebase.Firestore 获取第三方数据
问题描述
我正在尝试编写一个 Gutenberg Block 来从 Googles Firestore 加载一些数据。
这是我的第一个 ReactJS 项目,因为我认为这应该很容易:D 后端很好,并设置了所有需要的属性。我想我在这里只错过了一件小事,但这已经困扰了我好几天了。
我被困的地方是const getData = () => {...
在getDoc(...)
。似乎Promise<DocSnap>
永远得不到解决
import { initializeApp } from 'firebase/app';
import { getFirestore, doc, getDoc } from 'firebase/firestore';
const { render } = wp.element;
let state = '';
let data = '';
if ( typeof window.firebaseOptions !== 'undefined' ) {
// Initialize FirebaseApp
const firebaseApp = initializeApp( window.firebaseOptions );
state = 'initialized';
const db = getFirestore();
const docRef = doc( db, 'myDoc', window.docId );
const appendData = ( result ) => {
const text = document.createTextNode(
window.docId+ ': ' + result
);
data = text;
state = 'texted';
};
const getData = () => {
state = 'loading';
getDoc( docRef ).then( ( data ) => {
state = 'promised';
let result = '';
if ( doc.exists ) {
result = JSON.stringify( doc.data(), undefined, 2 );
} else {
result =
'No Doc with ID ' + window.docId + ' found.';
}
appendData( result );
} );
};
getData();
} else {
//TODO Warning message
}
const Block = ( { element } ) => (
<div >
<div element={ element } >Doc: { window.docId }</div>
<div element={ element } >state: { state }</div>
<pre>{ data }</pre>
</div>
);
window.addEventListener( 'DOMContentLoaded', () => {
const blocks = document.querySelectorAll( '.wp-block-my-wp-block' );
blocks.forEach( ( block ) => {
render(
<Block element={ block } />,
block
);
} );
} );
解决方案
推荐阅读
- google-bigquery - BigQuery 中的 T-SQL 游标交替出现
- swagger - 使用 Swagger 2.0 为原始属性创建不同类型
- python - xlsxwriter 引擎下的条件格式
- javascript - 未捕获的类型错误:$(...).cropper 不是函数 -cropper.js
- amazon-web-services - 使用 AWS Lambda/API Gateway 授权 google 服务账户
- android - Android - 将 Firebase 查询结果转换为字符串
- python - cpaste IPython 不粘贴内容
- iec61131-3 - IEC 61131-3 编程中的 SFC 步骤
- sql - oracle查询合并为一
- r - 如何在R中对双(数字)变量进行分类?我想创建一个二项式类别