Firebase reference objects by ID [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Imagine I have 2 collections: musicians and albums. Is there a chance I can reference albums with musicians by ID so that musicians can have multiple albums and appear as an array for musicians? What would the query look like in Firebase 9 Firestore? For example, this is possible and achievable in MongoDB. What are the chances of referencing 2 collections in Firebase? I am looking forward to achieving something like a Database Reference in MongoDB

You can use the Reference field type to add a reference to a document in a field. To use this, create a DocumentReference object for the destination:
const artistReference = doc(db, "artists", "beatles");
And then set this as the value of a field:
const songRef = doc(db, "songs", "get back");
await setDoc(songRef, {
title: "Get back",
artist: artistReference
});
Note that no referential integrity checks are performed by Firestore, so the above does not check whether the artist document exist - nor will it prevent deleting the artist document.

Related

how can we get the docs in users collection in firebase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i want to create an array from data coming from firestore.
the structure is as follows
users(collection)>uid1(doc)>(profile)>uid2(doc)>data
now the uid is dynamic as for every element of users collection. i want to create an array for every element with data
eg
[
{data},
{data},
.....
]
i have written the code for single uid
db.collection('users').doc("BbXn5M213XSKNfcSAR2MvbjPD403").collection("profile").onSnapshot((snapshot) => {
console.log(snapshot.docs.map(doc => doc.data()));
});
here are some firebase screenshot
screenshot 1 screenshot 2 here
I think what you'd looking for might be a collection group query, which is a way to get contents from all collections with a specific name.
So in your example this would be the way to get all documents from all data (sub)collections:
db.collectionGroup("profile").onSnapshot((snapshot) => {
console.log(snapshot.docs.map(doc => doc.data()));
});

Copy first 7 keys in object into a new object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
As the title suggests, how do I make a new object containing the 7 first keys of another object using JS? :) This is the structure of the object I would like to copy the data from.
{"data":[{"id":2338785,"team1":{"id":10531,"name":"Just For Fun"},"team2":{"id":10017,"name":"Rugratz"},"result":"2 - 0","event":{"name":"Mythic Cup 5","id":5148},"format":"bo3","stars":0,"date":1578279271000},....],"last_update":1578329378792}
Let's say there are 100 keys like this one, and I only want to copy the 7 first ones into a new object in JS.
Well technically, you have only 2 keys in the given Object but if you mean the data object, here's what you can do.
const MyNewObject = Object.entries(YourObject)
const results = []
and a simple for loop
MyNewObject.forEach((pair,i) => {
// for the 8th item it breaks and doesn't push it to results array
if ( i === 7 ) break;
results.push(pair)
}
OR u use slice instead :
// a little bit neater , and u call remove the empty results array above
const thisNewResult = MyNewObject.slice(0,6)
and finally the results is an array of key value pairs and you should do this code to make a new object from the results entries
const finalResults = Object.fromEntries(results)
Please notice that this may not be the Order you want since Object.Entries gives you the same order as the order of for in loop (for more info visit Elements order in a "for (… in …)" loop)

How to insert/update data in firebase using javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a firebase database where a node structure is like this.How do I insert/update data from a web page using javascript? I want to insert data individually in promo_ar,promo_en and promo_fr.
I am new in firebase and the help will be much appreciated!
You must do it this way
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
name_promo: value
});
Remember that using set, you update elements of a node of your database, on the other hand, when you perform this type of updates, you must send both elements to update within that node, if you omit any element, it is removed from your base, for example...
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
});
In this case, only desc_promo is being updated, thus eliminating the rest of the node elements.
in the same way you can insert new nodes in an element like this
firebase.database().ref('Promotions/promo_es').set({
desc_promo: value,
name_promo: value
});
in this way we add the Spanish language to the Promotions node
AGGREGATE:
you can have as many nodes as you want below a main node, remember that if you are going to update the final node, it does not affect the higher ones, but the higher node does affect the lower ones, for example
firebase.database().ref('Promotions/promo_es/exchange').set(
{
desc_promo: value,
name_promo: value
}
);
Promotions
|_>promo_es
|_>desc_promo : value
|_>name_promo : value
|_>exchange
|_>desc_promo : value //<-this is updated or created
|_>name_promo : value //<- this is updated or created
now if you edit the Promotions / promo_es node without including its sub nodes, these will be eliminated, you can perform the tests that you deem convenient in firebase to polish the method to use in your system

How to find that particular array Item is present or not Using JQuery + Backbone.js [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Each ArrayItem contains no. of Property like Id, Name, Description etc
But we want to Fetch ArrayItem with Help of Name Property .
So Please give me code suggestion in Jquery or backbonejs without using for loop.
If you are using BackboneJS, you already have UnderscoreJS installed. Underscore has several methods for searching through a collection. For instance, using _.findWhere(...):
var myArray = [ ... ];
var helpItem = _.findWhere(myArray, { name: 'help' });
This would return the first entry in the array that has a name property equal to 'help'. _.findWhere(...) can match multiple properties too. If you want to find an item with something other than direct equality of properties, you can use _.find(...):
var overTwentyOne = _.find(myArray, function(entry) {
return entry.age > 21;
});
This would return the first entry in the array whose age property was greater than 21.
Note also that most of the list-centric methods in Underscore are automatically mixed into all Backbone.Collection instances. So if you were searching through a Collection, the above findWhere(...) example could be more simply written as:
var helpItem = collection.findWhere({ name: 'help'});
This would return the first Backbone.Model instance from collection that had a property name equal to help.

Using Customer.io how to send 2 different emails simultaneously to two different users at the same time. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As the title states, I want to send two different emails, simultaneously to two different users.
I am using JavaScript for the API.
I also would like to know how can I send delayed emails, on custom dates. Currently we can do that from the dashboard, but that allows to fix days / weeks. But what I'm looking for is to calculate the dates and add a custom date delay that can't be predefined as given in the dashboard.
Thank you.
Well the problem has been solved.
I created two separate API calls for each customer. Using switch statement and while loop
switch(customer){
while(counter > 0){
case 1:
//Make first API call
//_cio.identify
_cio.identify({
// Required attributes
id: id, // Unique id for the currently signed in user in your application.
email: yourEmail, // Email of the currently signed in user.
created_at: time, // Timestamp in your system that represents when
// the user first signed up. You'll want to send it
// as seconds since the epoch.
// Optional (these are examples. You can name attributes what you wish)
task: task,
supervisor_email: supervisorEmail, // Add any attributes you'd like to use in the email subject or body.
goal_date: date, // To use the example segments, set this to 'free' or 'premium'.
user_name: name,
supervisor_name: supervisorName,
goal_setter: isUser,
pay: pay
});
customer = 2;
break;
case 2:
//Make the second API call
break;
counter--;
}
}

Categories

Resources