I am trying to alert this array using Firebase Realtime Database:
"discuss" : {
"taj" : {
"20200809" : {
"comment" : "first comment",
"full_date" : "2020/08/09"
}
}
}
I tried to do something like this:
data.val().children.forEach(children => {
alert(children.comment)
});
But came out with nothing.
Does anybody have an idea?
If you want to get comment from the database, then try the following:
let ref = firebase.database().ref("dicuss");
ref.child("taj").on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var comment = childSnapshot.val().comment;
console.log(comment);
});
});
Add a reference to the node taj then iterate and retrieve the comment attribute.
Check the docs:
https://firebase.google.com/docs/database/web/read-and-write
Related
Hello i am using mongoose.
I have built this query that finds my desired project :
const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : 'testing#email.com'} , { 'project.$.companyName': 1 });
this returns an object from my database like this :
{
'projectName' : 'x',
'companyName' : 'x bv'
}
How can i update the company name to be 'Y bv' instead of 'x bv'.
Assuming this is your document structure,
{
"_id" : ObjectId("5f2ae5a4b1549ac0460920dd"),
"projectName" : "A",
"project" : [
{
"companyName" : "T1",
"contactPerson" : {
"work_email" : "t1#gmail.com"
}
},
{
"companyName" : "T2",
"contactPerson" : {
"work_email" : "t2#gmail.com"
}
}
]
}
Single Update updateOne()
If you know email will be unique and want to update single document then use updateOne().
first is query part to find condition, email t1#gmail.com
second is set/update part, here $ is for array because project is an array, update companyName to T1 Company
await ClientManagers.updateOne(
{ 'project.contactPerson.work_email': 't1#gmail.com' },
{
$set: { "project.$.companyName": "T1 Companmy" }
}
)
Multiple Update updateMany()
If email is not unique and want to update everywhere then use updateMany(), it will update every matching documents.
await ClientManagers.updateMany(
{ 'project.contactPerson.work_email': 't1#gmail.com' },
{
$set: { "project.$.companyName": "T1 Company" }
}
)
Not suggesting update() method to use, because its deprecated in mongoose and will give Deprecation Warnings
, this function is replaced with updateOne(), updateMany() and replaceOne() methods.
Good start. Mongo has better documentation with examples. I suggest you to refer that also.
use update
db.collection.update({companyName:'x bv'}, {"$set":{"companyName":y}})
Mongo is case sensitive. So name should match exactly.
update updates one document. To update multiple, use updateMany or multi:true option with update or findOneAndMondify for one update for find and update case.
So I have a firebase realtime database which keeps users messages. At the moment I have it so when a users 'inbox' child is updated it retrieves the latest data for that child, for example:
firebase.database().ref('users/'+firebase.auth().currentUser.uid+'/inbox/').on('child_changed', function(snapshot) {
// get the latest data
});
But when using this method, if a message is deleted by the user, it will retrieve the child data again. Is there anyway to only retrieve the latest data only if a child inside of that child is updated?
So in my database I have it set out like this:
{
"users" : {
"IPenCEXKICROYWzYtyzybzhem9R2" : {
"inbox" : {
"32tcqIRaG2RAgb9dTwmcgKoVPhd2IPenCEXKICROYWzYtyzybzhem9R2" : {
"lastmessagetimestamp" : 1564139876817,
"conversationmessages" : {
"-LhKZ-MUVxbyRcqp-Q_c" : {
"from" : "32tcqIRaG2RAgb9dTwmcgKoVPhd2",
"messagetext" : "this is a test message",
}
},
},
},
},
}
}
I am saving that timestamp of the last message a user receives. So I would like it to only retrieve data if the 'lastmessagetimestamp' child value is changed.
The simplest way I could think of doing this would be:
firebase.database().ref('users/'+firebase.auth().currentUser.uid+'/inbox/**conversationid**/lastmessagetimestamp/').on('child_changed', function(snapshot) {
// get the latest data
});
But because my 'conversationid' is randomly generated and is not unique, it cannot be done. Is there a way to possibly skip the 'conversationid' and go directly to the 'lastmessagetimestamp'?
I'm working through a meteor tutorial : https://www.meteor.com/tutorials/blaze/collections
I have a collection defined,
Tasks = new Mongo.Collection("tasks");
I've added two items to it, one directly from meteor mongo command line, and another one using:
Tasks.insert({ text: "Testing JS", createdAt: new Date() });
Here are the results from running db.tasks.find() on the backend:
{ "_id" : ObjectId("559e9569abbb64fe1d5fd89a"), "text" : "Hello world!", "createdAt" : ISODate("2015-07-09T15:38:17.742Z") }
{ "_id" : "obRN8Rcssa9yJqXzA", "text" : "Testing JS", "createdAt" : ISODate("2015-07-09T17:00:13.285Z") }
But when I run Tasks.find({}); on the front end, I get empty result. It just gives me a long JSON but no data from the database.
In meteor, you can view the documents returned by a cursor by calling fetch on it. For example:
console.log(Tasks.find().fetch());
Looking at your code, you are not publishing anything:
if (Meteor.isServer) {
Meteor.publish("tasks", function () {
console.log(Tasks.find());
});
}
needs to be
if (Meteor.isServer) {
Meteor.publish("tasks", function () {
console.log(Tasks.find());
return Tasks.find();
});
}
or just remove it if you use autopublish.
Did you import the .js file of collection in front-end .js ?
import { Tasks } from '/your_path_goes_here/filename_goes_here.js';
If not import it and try below,
Add Publish like
if (Meteor.isServer) {
Meteor.publish('tasks', function task() {
return Task.find({});
});
}
Subscribe it in Front-End .js
Meteor.subscribe('task');
I have data in firebase that looks like this:
"application": {
"companies": {
"firebase": {
"creation": {
"name": "Firebase Inc",
"location": "USA"
},
"google": {
"creattion": {
"name": "Google Inc",
"location": "USA"
}
}
"facebook": {
},
"apple": {
}
}
}
}
There are tens of thousands of records under companies key. How do i efficiently execute following queries?
How do I query only the records for which key creation is present under their name?
How do I query only the records that DO NOT have key creation present under their name?
I also want to call .on('child_added') on the returned result set so that I can process only those specific records later on. Is it possible?
EDIT: Simpler way without using an extra parameter
Queries
Here are the queries to do this without having to use an extra parameter:
Find the companies without creation:
var ref = new Firebase(fbUrl+'/companies').orderByChild("creation").equalTo(null);
Find the companies with creation:
var ref = new Firebase(fbUrl+'/companies').orderByChild("creation").startAt(!null);
You would add ".indexOn": "creation" to the rules.
Edit 2: I was curious, so I pushed 11,000 records to /companies2 (half with creation children, half without). I was able to retrieve 5500 matching records in ~4 seconds using the above queries (or one of the variants I've shown below).
Edit 3: If you're running these queries frequently, it might be worth it to separate children of /companies into two bins based the presence of creation. That way, you can read the two segments separately without having to rely on queries.
Factory
Here is what the revised factory would look like (I've revised the PLNKR to match):
app.factory("CompaniesFactory",function($q, fbUrl){
return function(hasCreation){
var deferred = $q.defer();
var ref = new Firebase(fbUrl+'/companies').orderByChild("creation");
var query;
if (hasCreation) {
query = ref.startAt(!null);
// or:
// query = ref.startAt(true);
} else {
query = ref.equalTo(null);
// or:
// query = ref.endAt(!null);
// query = ref.endAt(true);
}
query.once("value", function(dataSnapshot){
deferred.resolve(dataSnapshot.val());
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
});
And yes, it is possible to call .on('child_added') on the returned dataSnapshot. See DataSnapshot.ref().
Original answer using an extra parameter:
(Keeping this for reference)
Another way to do it would be by adding another parameter called hasCreation to children of companies that have creation, and query by that.
Data
The query would then be var ref = new Firebase(fbUrl+'/companies').orderByChild("hasCreation").equalTo(hasCreation);
If hasCreation in the query is null, the query will return the companies without a hasCreation child.
If hasCreation in the query is true, the query will return the companies with hasCreation===true.
{
"company1" : {
"creation" : {
"name" : "company1"
},
"hasCreation" : true
},
"company2" : {
"name" : "company2"
},
"company3" : {
"name" : "company3"
},
"company4" : {
"creation" : {
"name" : "company4"
},
"hasCreation" : true
}
}
Rules
You would add the ".indexOn" : "hasCreation" to your rules like so:
"so:29179389":{
".read" : true,
".write" : true,
"companies" : {
".indexOn" : "hasCreation"
}
}
Companies Factory
app.factory("CompaniesFactory",function($q, fbUrl){
return function(hasCreation){
var deferred = $q.defer();
if (!hasCreation) {
hasCreation = null;
}
var ref = new Firebase(fbUrl+'/companies').orderByChild("hasCreation").equalTo(hasCreation);
ref.once("value", function(dataSnapshot){
deferred.resolve(dataSnapshot.val());
});
return deferred.promise;
}
});
Controller
app.controller('HomeController',function($scope,fbUrl,CompaniesFactory) {
$scope.getCompanies = function(hasCreation) {
var companies = new CompaniesFactory(hasCreation).then(function(data){
console.log(data);
$scope.companies = data;
});
}
});
HTML
<body ng-app="sampleApp">
<div ng-controller="HomeController">
<button ng-click="getCompanies(true)">Find with creation</button>
<button ng-click="getCompanies(false)">Find without creation</button>
<h2>Companies:</h2>
{{companies}}
</div>
</body>
What I would do, is I would set a condition to verify if your xxx.firebaseio.com/Application/companies/______/creation exists. In a empty blank, you can set for loop to irritate over the array of companies.Then, you can create two arrays with angular.forEach: one including those companies which do have 'creation', and the other array in which the elements do not include the 'creation'.
Hope that helps :)
Edit:
There is another approach to this question, in this thread:
Angularfire: how to query the values of a specific key in an array?
I'm using mongoose on my node app, and I want to get a seller by its email:
getSellerByEmail : function(email,next){
var Seller = mongoose.model('Seller');
console.log(inspect(email));
Seller.findOne({'email' : new RegExp(email, 'i')}, function(seller){
next(seller);
});
}
When I try to login, mongoose does not return the new user. But when I try to create another user with the same email, the server executes this function correctly and it returns the new user.
Also tried with {'email' : email} and It returns null, but when I do this query on mongo shell, it returns correctly.
db.sellers.findOne({email : 'email#email.email'});
{
"_id" : ObjectId("54b94759b042bdbf19cb7b97"),
"name" : "Nome da Empresa",
"cnpj" : "123123123",
"email" : "email#email.email",
"password" : "$2a$08$6UvW8Bux3CwUMok8ac12Sehbd.xCHnVUI51ZwhtGKBjkSa6/MrqUu",
"__v" : 0
}
I'm new to mongodb + mongoose, so I know it's a dumb question, but I just can't realize what is wrong... I've also created a findSellerById() function, and it works perfectly.
EDIT 1:
Using Mongoose debug, here's what it's printed:
Mongoose: sellers.findOne({ email: 'email#email.email' }) { fields: undefined }
Mongoose: sellers.findOne({}) { fields: undefined }
As you can see, also tried with no parameters, no success...
I had the same problem, maybe you could try this:
Seller.find({email: seller.email}, function(err, seller){
console.log(seller);
});
This solved mine, hope it will solve yours too !
The callback function passed into findOne takes two parameters (error and doc), so you're treating seller as the error parameter instead of the doc parameter.
So your function should look like this instead:
getSellerByEmail : function(email,next){
var Seller = mongoose.model('Seller');
console.log(inspect(email));
Seller.findOne({'email' : new RegExp(email, 'i')}, function(err, seller){
next(seller);
});
}