refer doc id using where condition in snapshotChanges() - firebase

I am fetching data using snapshotChanges(). I want to add where condition to it and fetch data of only 1 doc from collection.
Below is my code:
this.firestore.collection('notifications' , ref => ref
.where(document id , "==" , "1") // giving error
)
.snapshotChanges()
.subscribe(response => {
if(!response.length){
console.log("no data available");
return false;
}
this.notifyarr = [];
for(let item of response){
this.notifyarr.push(item.payload.doc.data());
console.log(this.notifyarr);
this.notification_length = this.notifyarr.length;
console.log(this.notification_length);
}
})
This line of code is giving error .where(document , "==" , "1") // giving error
How do we refer a document using .snapshotChanges() ?

If you want to query to check a documentid is equal to the value then you should do the following:
where(firebase.firestore.FieldPath.documentId(), "==" , "1")

If you know you only want a single document using its ID, why don't you just build a AngularFirstoreDocument to the desired document and call snapshotChanges on that reference?
this.firestore.collection('notifications').doc(id).snapshotChanges()...
No need to bother building a query that only gives you a single document.

Related

Variables won't change after executing once().then function in firebase ( Flutter )

I'm trying to get data from firebase and it worked with once().then function, but the problem is when I save the data a variable, its value will change only inside the function ( when the execution of the function is finished then the variable will not have the returned value from firebase )
code example :
static var temp = [];
void tempp() {
var db = FirebaseDatabase.instance.reference();
db.once().then((DataSnapshot snapshot) {
temp.add(1);
print(temp[0]);
});
print(temp[0]);
}
So, the first print statement will print the new value in temp list which is at index zero
but for the second print ( outside the once().then function ) will cause value range error
Invalid value: Valid value range is empty: 0
that means the value that was saved in the function it is not there anymore
How can I save the value after function is executed ?
I tried to use global variables it didn't work,
your help is much appreciated.
thank you.

Extracting sql data in ionic 5 without getting a ZoneAwarePromise

Using the ionic 5 framework I am trying to extract data from my SQLite database using a function:
getGlobal(name, db) {
console.log('... getting global');
var result;
sqlQuery = 'SELECT `value` FROM globals WHERE `name` = "' + name + '"';
console.log('sql query: ' + sqlQuery);
result = db.executeSql(sqlQuery, []).then(value => {
return JSON.parse(value.rows.item(0).value);
});
console.log('return: ', result);
return result;
}
I have tried even further ".then(data => {})" chaining to extract the correct result but withoug success. Always produces a ZoneAwarePromise.
Not sure where I am going wrong
The problem is not with the function itslef. Even converting it to an async function with await will return a fullfilled promise.
At the receiving end, eg
getGlobal('variable', db)
extract the value by adding
.then(value => console.log(value));
This is me learning more about promises. Maybe this answer is helpful to others.

how to write costomselect query which returns Future in Flutter/moor

I'm developing apps by flutter/moor.
but I don't get how to write custom query which returns Future<List> class object.
I could wrote query which returns Stream class object.
but it's not enough.
does anyone now how to change it to one which returns Future class.
query I wrote is bellow
db.dart
Stream<List<QuestionHeader>> selectQuestionHeaderByKey(int businessYear,int period, int questionNo) {
return
customSelect(
'SELECT *'
+'From question_Headers '
+'WHERE business_Year = ? '
+'AND period = ? '
+'AND question_No = ?;',
variables: [Variable.withInt(businessYear),Variable.withInt(period),Variable.withInt(questionNo)],
readsFrom: {questionHeaders},
).watch().map((rows) {
return rows
.map((row) => QuestionHeader(
businessYear:row.readInt('businessYear')
,period:row.readInt('period')
,questionNo:row.readInt('questionNo')
,subjectId:row.readInt('subjectId')
,compulsoryType:row.readInt('compulsoryType')
,answerType:row.readInt('answerType')
,questionText:row.readString('questionText')
,numberAnswer:row.readInt('numberAnswer')
,correctType1:row.readInt('correctType1')
,correctType2:row.readInt('correctType2')
,correctType3:row.readInt('correctType3')
,favorite:row.readBool('favorite')
)).toList();
});
}
this works but I need Future<List> class to return.

Firebase - How I can know first and last key

In my project, I must know first and last key of child to do something. I have query same below, I use 'i' to find first but I don't know how to get last key? Have any ways to set if Firebase query complete will do function with child_added? .In test, console.log(last) but is undefinded
var i = 0;
myDataRef.limitToLast(10).on('child_added', function (snapshot){
if( i == 0)
{
first = snapshot.key;
}
renderInfo(snapshot.key, snapshot.val(), 'new');
last = snapshot.key;
});
console.log(last)

Ext.JS Store Record is undefined

var debtProtectionId = 0
// get the selected id of debt protection dropdown
if (mainPanel.generalPanel.calculationsFieldSet.debtProtection.getValue() != '') {
debtProtectionId = mainPanel.generalPanel.calculationsFieldSet.debtProtection.getValue();
}
// get the store record with this id
var storeRecord = planCombinationsStore.getAt(debtProtectionId)
When I run the code it says 'storeRecord is undefined'.
What could be the cause of this?
Store.getAt expects an index to its internal collection. Do you mean Store.getById instead?

Resources