Nuxt Firebase using where - firebase

I am trying to return documents from firebase using nuxt firebase and need to include a where but I get the following error:
TypeError: this.$fireModule.firestore.FieldPath.assess is not a function. (In 'this.$fireModule.firestore.FieldPath.assess()', 'this.$fireModule.firestore.FieldPath.assess' is undefined)
From what I read I have to use this.$firemodule but still not working for me
My code is as follows:
this.$fire.firestore.collection('lights')
.where(this.$fireModule.firestore.FieldPath.assess(), '==', 'ZxBc9dMxVdodbHtnAf5g')
.get()
.then(snapshot => {...
I have enabled functions in my Nuxt.config.js

Overcomplicated it
.where('assess', '==', this.$route.params.id)
.get()
works

Related

Getting 'FirebaseError: Error compiling rules' on really simple rules definition in firebase emulator

summary
I'm getting an error with the firebase emulator trying to parse the simplest of firestore rules.
Given it's so simple I can't help think it might be some sort of versioning issue? but not really sure. I've tried to update firebase tools. I can't really find any similar errors on stackoverflow and elsewhere.
Hoping someone might have some ideas on this?
firestore.rules
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userID} {
allow read, write: if request.auth != null;
}
}
}
error by emulator parsing firestore.rules when trying to run 'npm test' / mocha test (see test below)
L5:11 Missing 'match' keyword before path.
L6:7 Unexpected 'allow'.
L6:7 mismatched input 'allow' expecting {'{', '/', PATH_SEGMENT}
L8:11 Missing 'match' keyword before path.
L9:7 Unexpected 'allow'.
L9:7 mismatched input 'allow' expecting {'{', '/', PATH_SEGMENT}
L9:28 Missing 'match' keyword before path.
L9:38 Forward slash '/' found where identifier or binding expected.
L9:39 mismatched input '$' expecting {'{', '/', PATH_SEGMENT}
L9:50 Missing 'match' keyword before path.
L9:66 Forward slash '/' found where identifier or binding expected.
L9:67 Unexpected '$'.
L9:68 Unexpected '{'.
L9:69 mismatched input 'request' expecting {'{', '/', PATH_SEGMENT}
L9:86 Unexpected ')'.
L15:1 Unexpected '}'.
at new FirestoreError (node_modules\#firebase\firestore\dist\index.node.cjs.js:1205:28)
at fromRpcStatus (node_modules\#firebase\firestore\dist\index.node.cjs.js:5240:12)
at fromWatchChange (node_modules\#firebase\firestore\dist\index.node.cjs.js:5476:35)
at PersistentListenStream.onMessage (node_modules\#firebase\firestore\dist\index.node.cjs.js:15743:27)
at C:\files\react-learning\app-02\test\node_modules\#firebase\firestore\dist\index.node.cjs.js:15676:30
at C:\files\react-learning\app-02\test\node_modules\#firebase\firestore\dist\index.node.cjs.js:15712:28
at C:\files\react-learning\app-02\test\node_modules\#firebase\firestore\dist\index.node.cjs.js:14143:20
at processTicksAndRejections (node:internal/process/task_queues:94:5)
unit test being run
const firebase = require('#firebase/testing')
const MY_PROJECT_ID = 'my-app-id'
describe('my app', () => {
it('can read users', async () => {
const db = firebase.initializeTestApp(
{
projectId: MY_PROJECT_ID,
auth: {
uid: '1231123',
email: 'someemail#blah.com'
}
}
).firestore()
const USER_ID = 'qnWc3u3UjowlF1PCVLSn'
const testDoc = db.collection('users').doc(USER_ID)
await firebase.assertSucceeds(testDoc.get())
})
})
It fails on the 'await firebase...' line.
what i'm expecting
The above firestore.rules to be parsed without any issues, so that the mocha test can run.
versions
firebase 9.5.0
Found the answer.
I needed to read in the firestore rules.
Was missing.
const fs = require('fs'); at the top, and
firebase.loadFirestoreRules({
projectId: MY_PROJECT_ID,
rules: fs.readFileSync('../firestore.rules', 'utf8')
})
...before I try to call any rules.
Pretty sure this wasn't in the YouTube video from Google on testing rules...but is in the docs => https://firebase.google.com/docs/rules/unit-tests about 2/3rds of the way down the page.

Firestore in Cloud Functions - Create a missing index through an error message

When using Firestore in Cloud Functions, how to use the Create a missing index through an error message feature?
Basically, after I use a new Firestore query in my Cloud Function, where should I expect to find the "error message [that] includes a direct link to create the missing index"?
Update: add sample code based on a comment suggestion
Assume the query below wasn't used before. After I deploy the function to Cloud Functions and use it, where can I find the automatically-created link for creating the missing index?
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
export const posts = functions.https.onCall(async (
data: { uid: string },
context
) => {
// ...
const querySnapshot = await postsRef
.where('uid', '==', data.uid)
.orderBy('timestamp', 'desc')
.get();
// ...
});
When you perform the query, the resulting promise will become rejected. Your code will need to catch that error from the rejected promise, then log the error object. The url will be in the log message. (I'm assuming you're using node here, but the same error handling will apply to any language using its conventions for capturing and logging errors.)

TypeError: admin.listUsers is not a function

I have deployed the following cloud function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.getAllUsers = functions.https.onRequest((req: any, res: any) => {
return admin.listUsers().then((userRecords: any) => {
userRecords.users.forEach((user: any) => console.log(user.toJSON()));
res.end('Retrieved all users successfully.');
}).catch((error: any) => console.log('Request failed: ', error));
})
But whenever I try to run it, I get the following error:
TypeError: admin.listUsers is not a function
at exports.getAllUsers.functions.https.onRequest (/srv/functions/lib/index.js:102:11)
at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/providers/https.js:57:9)
at process.nextTick (/srv/node_modules/#google-cloud/functions-framework/build/src/invoker.js:243:17)
at process._tickCallback (internal/process/next_tick.js:61:11)
How do I resolve it? And how do I add a parameter to paginate my data, i.e. to get 100 users at a time or something? I am expecting to call the endpoint as follows: curl http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers. How would it look like with a parameter i.e. 'http://localhost:5000/sweltering-fire-5301/us-central1/getAllUsers?limit=5&page=2'
If you imported the admin SDK like this:
const admin = require("firebase-admin")
Then listUsers is available to you like this:
admin.auth().listUsers()
You might want to check out the documentation and API reference, as they have everything you need to make this happen.

Getting could not invode RNFirebaseFirestore.documentSet null No virtual method Set

Please find the attached screenshot for exact error. I'm getting this error when I'm trying to set the data to firestore. Following is the code
import firebase from 'react-native-firebase';
export function initializeFirestore() {
firebase
.firestore()
.collection('communities')
.doc('communityname')
.collection('members')
.doc('memberid')
.set({ field1: 'value1' })
.then(function(docRef) {})
.catch(function(error) {
console.error('Error updating userChannel : ', error);
});
}
screenshot
I'm not sure if you got your answer but, the only workaround I've been seeing is to downgrade firebase-firestore to 17.1.5
in app level Build.gradle file add
implementation "com.google.firebase:firebase-firestore:17.1.5"
I got the answer from here

React native firebase database on value change not triggered

I have a react native app and using firebase database.
I try to use the on or once functions to get items from database but I don't know what I am doing wrong because I don't get any data.
This is my code to get data from firebase database:
firebase.database().ref("messages/").once('value', snapshot => {console.log("snapshot value", snapshot)});
I try the command from above also with on instead of once and same result. The console.log is never called.
When I am adding messages everything it's ok and the messages appear in the firebase console. For adding messages I am doing like this:
firebase.database().ref("messages/").push(message);
For firebase I am using this library: rnfirebase.io
This are the versions used:
React: 16.6.0-alpha.8af6728
React native: 0.57.4
React native firebase: 5.0.0
The Firebase once returns a promise so you have to use then after using value.
The code should be like
firebase.database().ref('tableNameHere').once('value').then(snapshot => {
if(snapshot.exists()){
console.log(snapshot.val());
}
}).catch(err => console.log(err));
when using on value the function will invoke on change in value like on CRUD operation
firebase.database().ref('tableNameHere').on('value', snapshot => {
if(snapshot.exists()){
console.log(snapshot.val());
}
},(err) => console.log(err))

Resources