Trying to get a node.js app to talk to my Realm App services. I am able to use the anonymous login and instantiate a currentUser but when I try to do a find query against a collection I get the following error.
Error: {“message”:“no rule exists for namespace ‘MyDB.myCollection’”,“code”:-1}
here are the rules for this collection
{
"roles": [
{
"name": "default",
"apply_when": {},
"insert": true,
"delete": true,
"search": true,
"write": true,
"fields": {},
"additional_fields": {}
}
],
"filters": []
}
what am I missing?
Hi Jesse,
Could you please provide more detail on how you are performing the query e.g. calling a Realm function etc.
Please accompany this with any relevant documentation you’re following.
Regards
Manny
Here is the code that sets up my realm client instance.
const realmClientService = ((): RealmClientService => {
const APP_ID: string = process.env.REALM_APP_ID || "UNDEFINED";
const REALM_GRAPHQL_ENDPOINT: string = `https://us-west-2.aws.realm.mongodb.com/api/client/v2.0/app/My-App-Id/graphql`;
let app: App;
const init = (): void => {
try {
app = new App({ id: APP_ID });
} catch (error) {
throw error;
}
};
const handleLogin = async (): Promise<User> => {
try {
if (!app) {
init();
}
const credentials = Credentials.anonymous();
await app.logIn(credentials).catch((error) => {
return Promise.reject(error);
});
return Promise.resolve(app.currentUser);
} catch (error) {
return Promise.reject(error);
}
};
const generateAuthHeader = async (): Promise<{ Authorization: string }> => {
try {
if (!app.currentUser) {
await handleLogin();
} else {
await app.currentUser.refreshCustomData();
}
const { accessToken } = app.currentUser as User;
return { Authorization: `Bearer ${accessToken}` };
} catch (error) {
return Promise.reject(error);
}
};
const getMongoClient = (): Services.MongoDB => {
return app.currentUser?.mongoClient(
"mongodb-atlas"
) as Services.MongoDB;
};
const getDb = (databaseName?: string): Services.MongoDBDatabase => {
const dbName = databaseName
? databaseName
: (process.env.MONGO_DATABASE as string);
const database = getMongoClient()?.db(dbName);
if (!database) {
throw new Error(`Could not find a database with the name ${dbName}`);
}
return database;
};
return {
login: handleLogin,
generateAuthHeader: generateAuthHeader,
get gqlEndPoint() {
return REALM_GRAPHQL_ENDPOINT;
},
get mongoClient() {
return getMongoClient();
},
db: getDb,
};
})();
export default realmClientService;
Here is the query
const data = await realmClientService
.db()
.collection("myCollection")
.find();
I discovered the problem. The database name I was using in my code was using the wrong case. My actual database name was like ‘myDatabase’ but I was using ‘MyDatabase’.
1 Like
system
(system)
Closed
May 18, 2022, 4:46pm
5
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.