尝试从 MongoDB Realm 数据库打印集合时出现此错误
INVALID_SESSION(realm::app::ServiceError:2):无效会话:访问令牌已过期
这是我的代码:
Realm.init(this);
app = new App(new AppConfiguration.Builder(Appid).build());
Credentials credentials = Credentials.emailPassword("XXXXX@gmail.com","XXXXX");
app.loginAsync(Credentials.anonymous(), new App.Callback<User>() {
@Override
public void onResult(App.Result<User> result) {
if(result.isSuccess())
{
Log.v("User","Logged In Successfully");
}
else
{
Log.v("User","Failed to Login");
}
}
});
User user = app.currentUser();
mongoClient = user.getMongoClient("mongodb-atlas");
mongoDatabase = mongoClient.getDatabase("doctordata");
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("data");
Document queryFilter = new Document().append("userid",user.getId());
RealmResultTask<MongoCursor<Document>> doc = mongoCollection.find(queryFilter).iterator();
final String[] da= new String[1];
doc.getAsync(task->{
if(task.isSuccess()){
MongoCursor<Document> results = task.get();
while (results.hasNext()){
Log.v("User",results.next().toJson());
}
}else{
Log.v("User",task.getError().toString());
}
});
}catch(Exception e){
e.printStackTrace();
}
```