我是 ReactNative 的新手,我正在尝试将 MongoDB Atlas 连接到我的 RN 应用程序。default app client has not yet been initialized/set
即使我正在调用该Stitch.initializeDefaultAppClient(MONGODB_APP_CLIENT)
方法,我也会收到一条错误消息。
在 App.js 中:
import { Stitch, AnonymousCredential } from "mongodb-stitch-react-native-sdk";
...
_loadClient() {
Stitch.initializeDefaultAppClient(MONGODB_APP_CLIENT).then(client => {
this.setState({ client });
this.state.client.auth
.loginWithCredential(new AnonymousCredential())
.then(user => {
console.log(`Successfully logged in as user ${user.id}`);
this.setState({ currentUserId: user.id });
this.setState({ currentUserId: client.auth.user.id });
})
.catch(err => {
console.log(`Failed to log in anonymously: ${err}`);
this.setState({ currentUserId: undefined });
});
});
}
}
...
在 Contacts.js 中:
import { Stitch, RemoteMongoClient } from "mongodb-stitch-react-native-sdk";
..
constructor() {
super();
this.state = {
isLoading: true,
users: []
};
this._loadClient = this._loadClient.bind(this);
}
componentDidMount() {
this._loadClient();
}
_loadClient() {
const stitchAppClient = Stitch.defaultAppClient;
const mongoClient = stitchAppClient.getServiceClient(
RemoteMongoClient.factory,
"mongodb-atlas"
);
const db = mongoClient.db("dbapp");
const users = db.collection("users");
users
.find({ status: "new" }, { sort: { date: -1 } })
.asArray()
.then(docs => {
this.setState({ users });
})
.catch(err => {
console.warn(err);
});
}
...
上面,我分享了这个工作所必需的代码。我的代码中包含所有内容,即使我初始化了应用程序客户端,我仍然收到错误消息。会喜欢我能在这里得到的所有帮助!
完整的错误信息:
Error: default app client has not yet been initialized/set
This error is located at:
in Contacts (at SceneView.js:9)
in SceneView (at StackViewLayout.tsx:910)
in RCTView (at View.js:45)
in View (at createAnimatedComponent.js:151)
in AnimatedComponent (at StackViewCard.tsx:106)
in RCTView (at View.js:45)
in View (at createAnimatedComponent.js:151)
in AnimatedComponent (at screens.native.js:71)
in Screen (at StackViewCard.tsx:93)
in Card (at createPointerEventsContainer.tsx:95)
in Container (at StackViewLayout.tsx:985)
in RCTView (at View.js:45)
in View (at screens.native.js:101)
in ScreenContainer (at StackViewLayout.tsx:394)
in RCTView (at View.js:45)
in View (at createAnimatedComponent.js:151)
in AnimatedComponent (at StackViewLayout.tsx:384)
in PanGestureHandler (at StackViewLayout.tsx:377)
in StackViewLayout (at withOrientation.js:30)
in withOrientation (at StackView.tsx:104)
in RCTView (at View.js:45)
in View (at Transitioner.tsx:267)
in Transitioner (at StackView.tsx:41)
in StackView (at createNavigator.js:81)
in Navigator (at createKeyboardAwareNavigator.js:12)
in KeyboardAwareNavigator (at SceneView.js:9)
in SceneView (at createTabNavigator.js:39)
in RCTView (at View.js:45)
in View (at ResourceSavingScene.js:37)
in RCTView (at View.js:45)
in View (at ResourceSavingScene.js:26)
in ResourceSavingScene (at createBottomTabNavigator.js:120)
in RCTView (at View.js:45)
in View (at screens.native.js:101)
in ScreenContainer (at createBottomTabNavigator.js:110)
in RCTView (at View.js:45)
in View (at createBottomTabNavigator.js:109)
in TabNavigationView (at createTabNavigator.js:197)
in NavigationView (at createNavigator.js:81)
in Navigator (at SceneView.js:9)
in SceneView (at SwitchView.js:12)
in SwitchView (at createNavigator.js:81)
in Navigator (at createAppContainer.js:430)
in NavigationContainer (at App.js:38)
in RCTView (at View.js:45)
in View (at App.js:36)
in App (at withExpoRoot.js:20)
in RootErrorBoundary (at withExpoRoot.js:19)
in ExpoRootComponent (at renderApplication.js:35)
in RCTView (at View.js:45)
in View (at AppContainer.js:98)
in RCTView (at View.js:45)
in View (at AppContainer.js:115)
in AppContainer (at renderApplication.js:34)
Function.get [as defaultAppClient]
106aa394-9652-42d9-8b6b-cda0fd593b63:183243:17
Contacts._loadClient
106aa394-9652-42d9-8b6b-cda0fd593b63:152692:67
Contacts.proxiedMethod
106aa394-9652-42d9-8b6b-cda0fd593b63:47391:32
Contacts.componentDidMount
106aa394-9652-42d9-8b6b-cda0fd593b63:152685:14
Contacts.proxiedComponentDidMount
106aa394-9652-42d9-8b6b-cda0fd593b63:47404:42
commitLifeCycles
106aa394-9652-42d9-8b6b-cda0fd593b63:21888:28
commitAllLifeCycles
106aa394-9652-42d9-8b6b-cda0fd593b63:23239:13
Object.invokeGuardedCallbackImpl
106aa394-9652-42d9-8b6b-cda0fd593b63:11985:16
invokeGuardedCallback
106aa394-9652-42d9-8b6b-cda0fd593b63:12076:37
commitRoot
106aa394-9652-42d9-8b6b-cda0fd593b63:23433:13
作为更新,此错误是随机发生的,并且随机不发生。一分钟加载我的数据,下次我保存文件(并且应用程序重新加载)时,我再次收到错误。重新加载一堆最终修复它。但是,当然,它会再次发生。