- Published on
- ·2 min read
React Native - Resolving Realm(BSON) WARN
I apologize in advance for any awkward expressions in English. 🙏
English is not my native language, and I have relied on ChatGPT's assistance to proceed with the translation.
WARNING Message
While using Realm with React Native and the realm.create
method, I encountered a WARNING message:
The WARNING message is as follows:
BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.
The code where the WARNING occurred is as follows:
realm.write(() => {
realm.create('Schema', {
_id: new Realm.BSON.ObjectId(),
message: 'Hello World',
});
});
The WARNING message appears to be related to the usage of new Realm.BSON.ObjectId()
.
Installing react-native-get-random-values
First, follow the instructions provided in the WARN message's link to install it.
npm install react-native-get-random-values
It appears that the same WARN message is still occurring.
Add import 'react-native-get-random-values'
import 'react-native-get-random-values';
import Realm from 'realm';
You should add the import 'react-native-get-random-values' statement before importing Realm.
This should resolve the WARN message.
❗ Please note that the order is important; if you import it after Realm, you may still encounter the same WARN message.