INTERNAL ASSERTION FAILED -在React JS中将文档添加到集合时出现firebase错误.&



我正在使用React JS和Firebase创建一个聊天应用程序,其中我有一组组,其中我们有每个组单独的消息集合。对于在Firestore中添加新组,它工作得很好,但是当我向各自的集合添加新消息时,它显示此错误:

Uncaught (in promise) Error: FIRESTORE (8.8.0) INTERNAL ASSERTION FAILED: Unexpected 
state
at O (prebuilt-e10b3b00-01f089b3.js:287)
at ce (prebuilt-e10b3b00-01f089b3.js:1978)
at prebuilt-e10b3b00-01f089b3.js:1957
at comparator (prebuilt-e10b3b00-01f089b3.js:11535)
at t.insert (prebuilt-e10b3b00-01f089b3.js:2825)
at t.insert (prebuilt-e10b3b00-01f089b3.js:2668)
at t.add (prebuilt-e10b3b00-01f089b3.js:11579)
at prebuilt-e10b3b00-01f089b3.js:12042
at t.inorderTraversal (prebuilt-e10b3b00-01f089b3.js:2802)
at t.inorderTraversal (prebuilt-e10b3b00-01f089b3.js:2720)
at t._o (prebuilt-e10b3b00-01f089b3.js:12016)
at prebuilt-e10b3b00-01f089b3.js:12338
at step (tslib.es6.js:102)
at Object.next (tslib.es6.js:83)
at tslib.es6.js:76
at new Promise (<anonymous>)
at __awaiter (tslib.es6.js:72)
at prebuilt-e10b3b00-01f089b3.js:12333
at t.Oo (prebuilt-e10b3b00-01f089b3.js:12357)
at prebuilt-e10b3b00-01f089b3.js:12808
at prebuilt-e10b3b00-01f089b3.js:6794
at G (prebuilt-e10b3b00-01f089b3.js:598)
at t.forEach (prebuilt-e10b3b00-01f089b3.js:6791)
at prebuilt-e10b3b00-01f089b3.js:12807
at step (tslib.es6.js:102)
at Object.next (tslib.es6.js:83)
at tslib.es6.js:76
at new Promise (<anonymous>)
at __awaiter (tslib.es6.js:72)
at Ys (prebuilt-e10b3b00-01f089b3.js:12802)
at prebuilt-e10b3b00-01f089b3.js:12462
at step (tslib.es6.js:102)
at Object.next (tslib.es6.js:83)
at fulfilled (tslib.es6.js:73)

我搜索了整个互联网,并了解到timeStamp可能造成了这个问题。如能提供任何帮助,我将不胜感激。

下面是我的代码:
const ChatInput = ({ roomId }) => {
const [showKeyboard, setShowKeyboard] = useState(false);
const [message, setMessage] = useState('');
const emoRef = useRef(null);
const onEmojiClick = (event, emojiObject) => {
setMessage(message + emojiObject?.emoji);
};
// useEffect(() => {
//  if (message === '') {
//      inputRef.current.focus();
//  }
// }, []);
const sendMessage = (e) => {
e.preventDefault();
// console.log(message);
db.collection('rooms').doc(roomId).collection('messages').add({
message,
user: 'Remy Sharp',
userImage:
'https://www.webxcreation.com/event-recruitment/images/profile-1.jpg',
timeStamp: firebase.firestore.FieldValue.serverTimestamp(),
});
setMessage('');
};
return (
<ChatInputWrapper>
<Tooltip title="Emoji" TransitionComponent={Zoom}>
<Emoji onClick={() => setShowKeyboard(!showKeyboard)}>
<InsertEmoticonIcon />
</Emoji>
</Tooltip>
<EmojiKeyboard ref={emoRef} show={showKeyboard}>
<Picker onEmojiClick={onEmojiClick} />
</EmojiKeyboard>
<ChatForm onSubmit={sendMessage}>
<ChatText
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="Type a message ..."
/>
<Tooltip title="Send" TransitionComponent={Zoom}>
<ChatSend type="submit" onClick={sendMessage}>
<SendIcon />
</ChatSend>
</Tooltip>
</ChatForm>
</ChatInputWrapper>
);
};
export default ChatInput;

下面是firebase.js:

import firebase from 'firebase';
var firebaseConfig = {
apiKey: '',
authDomain: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: '1::web:',
measurementId: 'G',
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
export { db, auth, provider };

这是因为

export { db, auth, provider };

不输出firebaseApp(也不输出firebase),因此

firebase.firestore.FieldValue.serverTimestamp();

生成错误。


我建议你这样做:

在firebase.js:

import firebase from 'firebase';
var firebaseConfig = {
// ...
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
const fv = firebase.firestore.fieldValue;  // <=
const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
export { db, fv, auth, provider };

当写入Firestore时:

db.collection('rooms').doc(roomId).collection('messages').add({
// ...
timeStamp: fv.serverTimestamp(),
});

FieldValues,例如FieldValue.serverTimestamp(),是在使用set()update()方法编写文档字段时可以使用的前哨值。

如果你想在add()方法中使用时间戳,你可以使用firebase.firestore.Timestamp.now()

相关内容

  • 没有找到相关文章

最新更新