Firebase lazy load - javascript

I'm trying to lazy load firebase items to later on load more of them whenever user reaches end of div container. When i remove .endAt() and .startAt() i'm receiving the 15 items though they are not beeing incremented and it's stuck at these 15 items.
When i keep .endAt() and .startAt() i'm receiving firebase warning
Using an unspecified index. Consider adding ".indexOn": "title" at /items even though .indexOn is set. I'm confused by that warning. Thanks in advance for any help.
Firebase structure
{
"items" : {
"-Kk6aHXIyR15XiYh65Ht" : {
"author" : "joe",
"title" : "Product 1"
},
"-Kk6aMQlh6_E3CJt_Pnq" : {
"author" : "joe",
"title" : "Product 2"
}
},
"users" : {
"RG9JSm8cUndpjMfZiN6c657DMIt2" : {
"items" : {
"-Kk6aHZs5xyOWM2fHiPV" : "-Kk6aHXIyR15XiYh65Ht",
"-Kk6aMTJiLSF-RB3CZ-2" : "-Kk6aMQkw5bLQst81ft7"
},
"uid" : "RG9JSm8cUndpjMfZiN6c657DMIt2",
"username" : "joe"
}
}
}
Security rules
{
"rules": {
".read": true,
".write": "auth != null",
"users":{
"$uid": {
".write": "$uid === auth.uid"
"items":{
".indexOn": "title",
"$itemId": {
"title": {".validate": "...}
"type": {".validate": "...}
}
}
}
}
}
}
}
Code structure for lazy load
let _start = 0,
_end = 14,
_n = 15;
function lazyLoadItems(){
firebase.database().ref('items')
.orderByChild('title')
.startAt(_start)
.endAt(_end)
.limitToFirst(_n)
.on("child_added", snapshot=> console.log(snapshot.val()));
_start += _n;
_end += _n;
}

You're misunderstanding how Firebase queries work. It's easiest to see if you use hard-coded values:
firebase.database().ref('items')
.orderByChild('title')
.startAt(0)
.endAt(14)
.limitToFirst(15)
There is no item with title=0 or title=14, so the query doesn't match anything.
Firebase Database queries match on the value of the property you order on. So when you order by title the values you specify in startAt and endAt must be titles. E.g.
ref.child('items')
.orderByChild('title')
.startAt("Product 1")
.endAt("Product 1")
.limitToFirst(15)
.on("child_added", function(snapshot) { console.log(snapshot.val()); });
See for the working sample of this: http://jsbin.com/hamezu/edit?js,console
To implement pagination, you'll have to remember the last item of the previous page and pass that in to the next call: startAt(titleOfLastItemOnPreviousPage, keyOfLastItemOnPreviousPage).

startAfter is the last element
each time you have to send the last element:
const lastAlert = querySnapshot.docs[querySnapshot.docs.length - 1]
here an example:
export const fetchAlerts = (createdForId, startAfter) => {
return new Promise((resolve, reject) => {
firebase
let query = firebase
.firestore()
.collection('alerts')
.where('generated_for.uid', '==', createdForId)
.where('read', '==', false)
.orderBy('created_at', 'desc')
.limit(25)
if (startAfter) {
query = query.startAfter(startAfter)
}
query
.get()
.then(querySnapshot => {
const lastAlert = querySnapshot.docs[querySnapshot.docs.length - 1]
const alerts = querySnapshot.docs.map(doc => ({
...doc.data(),
uid: doc.id,
}))
resolve(alerts)
resolve({ alerts, lastAlert })
})
.catch(error => {
console.log(error.message)

Related

Firebase cloud function finished before complete the foreach

I want to update the list of "Reservations" with some new variables. But I can't make it work because the function ends before updating. I know the problem is because I make asynchronous calls but I can't solve it. How can I use Promise in this case?
I edited the code using Promise but still does not update the "Reservations"
Returns {"data": null} to my website console
In the firebase console does not generate any errors. Return the message "reservations actualized"
exports.actualizeReservations = functions.https.onCall((data, response) => {
var promisesUpdateReservations = [];
return admin.database().ref(Constants.RESERVATIONS).once("value")
.then((snapshot) => {
console.log("RESERVATIONS: " + snapshot)
snapshot.forEach((reserveSnap) => {
var reserve = reserveSnap.val();
if(reserve.reservationId && reserve.restoId) {
admin.database().ref(`${Constants.RESTAURANTS}/${reserve.restoId}`).once("value")
.then((restoSnap) => {
if(restoSnap.exists() && restoSnap.val() !== null) {
var resto = restoSnap.val();
if(resto.address && !resto.address.fullAddress) {
var restoAddress = resto.address ? {
street: resto.address.street ? resto.address.street : "",
fullAddress: resto.address.fullAddress ? resto.address.fullAddress : "",
city: resto.address.city ? resto.address.city : "",
country: resto.address.country ? resto.address.country : "",
postalCode: resto.address.postalCode ? resto.address.postalCode : "",
province: resto.address.province ? resto.address.province : "",
l: resto.l ? resto.l : null,
g: resto.g ? resto.g : null,
} : null;
var restoUserIDs = `${reserve.restoId}/${reserve.userId}`;
const promiseUpdateReservation = admin.database().ref(`${Constants.RESERVATIONS}/${reserve.reservationId}`).update({
restoAddress,
restoUserIDs
})
promisesUpdateReservations.push(promiseUpdateReservation)
}
}
})
.catch((err) => {
console.log("resto not found")
});
}
});
Promise.all(promisesUpdateReservations)
.then(() => {
console.log("reservations actualized");
return { code: 0, result: "successful", description: "reservations actualized" };
})
})
.catch((err) => {
console.log("get restaurants error: " + err);
return { code: 1, result: "successful", description: "reservations not loaded" };;
});
})
This function is going to complete immediately every time it's invoke, and likely perform none of its work. This is because it's not returning anything at all from the top-level function callback. Instead, you need to return a promise from the top level of the function that resolves with the data to send to the client only after all the async work is complete (and you're kicking off a lot of it here). Proper handling of promises is critical to making Cloud Functions work correctly.

Retrieve n random document with certain filters MongoDB

I need to retrieve from a collection of random docs based on a limit given.
If some filters are provided they should be added to filter the results of the response. I'm able to build the match and size based on the fields provided but even tho I have 20 documents that meet the filter when I make the call I receive only 2 or 3 docs back and I can't seem to figure it out. If I set only the limit it does give me back N random docs based on the limit but if I add a filter it won't give me the wanted results.
This is what I do now
const limit = Number(req.query.limit || 1);
const difficulty = req.query.difficulty;
const category = req.query.category;
const settings = [
{
$sample: {
size: limit
}
}
];
if (difficulty && category) {
settings.push({
$match: {
difficulty: difficulty,
category: category
}
});
} else if (difficulty && category == null) {
settings.push({
$match: {
difficulty
}
});
}
if (difficulty == null && category) {
settings.push({
$match: {
category
}
});
}
console.log(settings);
Question.aggregate(settings)
.then(docs => {
const response = {
count: docs.length,
difficulty: difficulty ? difficulty : "random",
questions:
docs.length > 0
? docs.map(question => {
return {
_id: question._id,
question: question.question,
answers: question.answers,
difficulty: question.difficulty,
category: question.category,
request: {
type: "GET",
url:
req.protocol +
"://" +
req.get("host") +
"/questions/" +
question._id
}
};
})
: {
message: "No results found"
}
};
res.status(200).json(response);
})
.catch(err => {
res.status(500).json({
error: err
});
});
Order of the stages matters here. You are pushing the $match stage after the $sample stage which first put the $size to whole the documents and then applies the $match stage on the $sampled documents documents.
So finally you need to push the $sample stage after the $match stage. The order should be
const limit = Number(req.query.limit || 1);
const difficulty = req.query.difficulty;
const category = req.query.category;
const settings = []
if (difficulty && category) {
settings.push({
$match: {
difficulty: difficulty,
category: category
}
})
} else if (difficulty && category == null) {
settings.push({
$match: {
difficulty
}
})
}
if (difficulty == null && category) {
settings.push({
$match: {
category
}
})
}
setting.push({
$sample: {
size: limit
}
})
console.log(settings);
Question.aggregate(settings)

Trouble filtering data in firebase database

I'm trying to filter some data from firebase database in a cloud function.
That data looks like this :
"items": {
"id1": {
"status": {
"creation": timestampValue,
"status": "initialized"
},
"data" : data1
}
"id2": {
"status": {
"status": "loaded"
},
"data" : data2
},
"id2": {
"status": {
"creation": timestampValue,
"status": "loaded"
},
"data" : data
},
"id3": {
"status": {
"creation": timestampValue,
"status": "ended"
},
"data" : data3
}
}
I want to use a filter based on the creation field.
The field is not always present.
My code is inspired by this one :
https://github.com/firebase/functions-samples/blob/master/delete-old-child-nodes/functions/index.js
here's the code I wrote :
const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 24 Hours in milliseconds.
exports.cleanAfter24h = functions.database.ref('/items/{itemId}').onUpdate((change, event) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = now - CUT_OFF_TIME;
const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff);
return oldItemsQuery.once('value').then((snapshot) => {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
let childData = child.val();
if (childData.status.creation) {
let elapsed = childData.status.creation - cutoff;
if (elapsed <= 0) {
updates[child.key] = null;
}
} else {
console.log(child.key + ' does not have a creation date');
}
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});
When the code is run, all items are retrieved, even those that have a timestamp smaller than the cutoff and those that do not have a creation field.
Any suggestions how to fix that?
I tried removing the items that have no creation field by adding a startAt("") before the endAt as suggested here :
Firebase, query with "a child exists" as a condition?
const oldItemsQuery = ref.orderByChild('creation')startAt("").endAt(cutoff);
Doing this I have no results in the query response.
Change this:
const oldItemsQuery = ref.orderByChild('creation').endAt(cutoff);
into this:
const oldItemsQuery = ref.orderByChild('creation').equalTo(cutoff);
equalTo
Creates a Query that includes children that match the specified value.
Using startAt(), endAt(), and equalTo() allows us to choose arbitrary starting and ending points for our queries.

How to Tally Up Numerical Values and Produce One Value for all Documents in Mongo/Node

I am trying to do what should be a rather simple operation in my mongoDB/Node environment. Every document in the collection I'm targeting has a field "openBalance", which is a number value. All I want to do is find the totalOpenBalance by adding all of those together.
So far, in reviewing the MongoDB documentation, both $add and $sum seem to be used to perform an operation on the individual documents within the collection, rather than on the collection itself.
This leads me to wonder, is there a different way I should approach this? I've tried numerous constructions, but none work. Here is my function in full:
exports.getClientData = async function (req, res, next) {
let MongoClient = await require('../config/database')();
let db = MongoClient.connection.db;
let search, skip, pagesize, page, ioOnly = false, client;
let docs = [];
let records = 0;
if (_.isUndefined(req.params)) {
skip = parseInt(req.skip) || 0;
search = JSON.parse(req.search);
pagesize = parseInt(req.pagesize) || 0;
page = parseInt(req.page) || 0;
client = req.client || '';
ioOnly = true;
}
else {
skip = parseInt(req.query.skip) || 0;
search = req.body;
pagesize = parseInt(req.query.pagesize) || 0;
page = parseInt(req.query.page) || 0;
client = req.query.client || '';
}
search = {};
if (skip === 0) {
skip = page * pagesize;
}
if (client) {
let arrClient = [];
arrClient = client.split(",");
if (arrClient) {
// convert each ID to a mongo ID
let mongoArrClient = arrClient.map(client => new mongo.ObjectID(client));
if (mongoArrClient) {
search['client._id'] = { $in: mongoArrClient };
}
}
}
console.log(search);
let counter = 0;
let count = await db.collection('view_client_data').find(search).count();
let totalClients = await db.collection('view_client_data').find(search).count({ $sum: "client._id" });
console.log('totalClients', totalClients);
let totalOpenBalance = await db.collection('view_client_data').find(search).count({ $sum: { "$add" : "openBalance" } });
console.log('totalOpenBalance', totalOpenBalance);
db.collection('view_client_data').find(search).skip(skip).limit(pagesize).forEach(function (doc) {
counter ++; {
console.log(doc);
docs.push(doc);
}
}, function (err) {
if (err) {
if (!ioOnly) {
return next(err);
} else {
return res(err);
}
}
if (ioOnly) {
res({ sessionId: sessID, count: count, data: docs, totalClients: totalClients, totalOpenBalance: totalOpenBalance });
}
else {
res.send({ count: count, data: docs, totalClients: totalClients, totalOpenBalance: totalOpenBalance });
}
});
}
As you can see in the above code, I am getting the total number of clients with this code:
let totalClients = await db.collection('view_client_data').find(search).count({ $sum: "client._id" });
console.log('totalClients', totalClients);
That works perfectly, adding up the instances of a client and giving me the total.
Again, to be crystal clear, where I'm running into a problem is in summing up the numerical value for all of the openBalance values. Each document has a field, openBalance. All I want to do is add those up and output them in a variable titled totalOpenBalance and pass that along in the response I send, just like I do for totalClients. I have tried numerous options, including this:
let totalOpenBalance = await db.collection('view_client_data').find(search).count({ $sum: { "$add" : "openBalance" } });
console.log('totalOpenBalance', totalOpenBalance);
and this:
let totalOpenBalance = await db.collection('view_client_data').find(search).aggregate({ $sum: { "$add" : "openBalance" } });
console.log('totalOpenBalance', totalOpenBalance);
... but as I say, none work. Sometimes I get a circular reference error, sometimes an aggregate is not a function error, other times different errors. I've been wracking my brain trying to figure this out -- and I assume it shouldn't be that complicated once I understand the required syntax. How can I get my totalOpenBalance here?
By the way, the documents I'm targeting look something like this:
{
"_id": "3hu40890sf131d361f1ad908",
"client": {
"_id": "4ft9d366121j04563be0b01d6",
"name": {
"first": "John",
"last": "Smith"
}
},
"openBalance": 128,
"lastPurchaseDate": "2018-01-19T00:00:00.000Z"
},
$sum is an accumulator operator that must appear within a $group or $project aggregate pipeline stage. To also incorporate your search filter, you can include a $match stage in your pipeline.
let result = await db.collection('view_client_data').aggregate([
{$match: search},
{$group: {_id: null, totalOpenBalance: {$sum: '$openBalance'}}}
]).next();
console.log(result.totalOpenBalance);
I think $group is what you're looking for.
So for example to calculate all the openBalance fields
db.view_client_data.aggregate(
[
{
$group: {
_id : null
totalOpenBalance: { $sum: "$openBalance" },
}
}
]
)
this should give you an object back like {totalOpenBalance: 900}
Here is the mongodb documentation for some more examples
https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group

Update Data from Within a Loop

I am trying to get my nodejs controller to update the rate in the currency table.
Everything works fine in S3T / RoboMongo, but for some reason it just wont fire the update inside the nodejs controller.
Here is my currency table
{
"_id" : "USD",
"index" : NumberInt(6),
"name" : "Dollar",
"currency" : "USD",
"symbol" : "$",
"active" : true,
"default" : false,
"rate" : 0
}
{
"_id" : "EUR",
"index" : NumberInt(2),
"name" : "Euro",
"currency" : "EUR",
"symbol" : "€",
"active" : true,
"default" : false,
"rate" : 0
}
I tried both of these, works fine in S3T but not inside nodejs:
db.currency.update (
{ _id : "EUR" },
{ $set: { rate : 123 }},
{ upsert: true }
)
db.currency.updateOne (
{ _id : "EUR" },
{ $set: { rate : 123 }},
{ upsert: true }
)
Here is my nodejs code:
var mongoose = require('mongoose');
var currencyModel = require('../models/currencyModel');
var currencyTable = mongoose.model('currencyModel');
var updateRates = () => {
return new Promise((resolve, reject) => {
for (var key in data.quotes) {
var currencyID = key.substring(3);
var newRate = (data.quotes[key] * THBUSD).toFixed(5);
console.log("currencyID: " + currencyID)
console.log("newRate: " + newRate)
currencyTable.update (
{ _id: currencyID },
{ $set: { rate : newRate }},
{ upsert: true }
),function (err, data) {
if (err) {
reject(new Error('updateRates: ' + err));
};
};
};
resolve();
})};
And here is my currencyModel (which is where I think the problem is?!?)
// Currency Model
// This model is the structure containing data from the Currency table
//
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var currencySchema = new Schema({
_id: String, // Unique Currency code
index: Number, // Indes for sorting
name: String, // Currency name
symbol: String, // Currency symbol
active: Boolean, // Active True False
rate: Number // Exchange rate (multiply with THB price)
});
module.exports = mongoose.model('currencyModel', currencySchema, 'currency');
I cannot see why it wont fire the currencyTable.update from inside nodejs.
I turned debug on in mongoose, and I see all other mongodb operations in the console like Mongoose: price.findOne({ _id: 'ATL-D406' }, { fields: {} }) etc.. but I do not see this currency.update in the console, which is why I dont think its fired off to mongodb - and I cannot see the reason.
You have a "loop" that completes execution before the inner callbacks fire. Instead just use Promises all the way through and call Promise.all() to collect all the iterated Promises and resolve them:
var updaterates = () => {
return Promise.all(
Object.keys(data.quotes).map(k => {
return currencyTable.update(
{ _id: k.substring(0,3) },
{ $set: { rate : (data.quotes[k] * THBUSD).toFixed(5) }},
{ upsert: true }
).exec()
});
)
};
The returned response of Promise.all() is an array of the response objects from the updates. Also note that this is a "fast fail" operation and calls will be made in parallel.
Object.keys() returns an "array of the key names" in the specified object. .map() iterates those keys and returns an "array" of the return value for the iterator.
We use the k as the "key name" to access the wanted key from data.quotes and use the values to perform each .update() with .exec() to return a "real" Promise. The iterator returns an "array" Promise which becomes the argument to Promise.all().

Categories

Resources