Firebase js library retrieves empty data from non empty database - javascript

steps to reproduce:
Initialize your firebase db to:
{
"restaurants" : [ {
"address" : "via Rettifilo, 34 Castelguidone CH Italy",
"title" : "Dama & Dama"
}, {
"address" : "Corso Umberto I, 3 Castelguidone CH Italy",
"title" : "Bar sabatino"
} ]
}
Check your read/write firebase persmission rules to be
{
"rules": {
".read": true,
".write": true
}
}
Execute this js code:
firebase.inizializeApp(yourJsonConfig)
\*code*\
firebase.database().ref().once('value').then(function(snapshot){
console.log(snapshot.val()); \\ this will print Array(0)
})
Expected behaviour: console logs "{'restaurants' ........}"
Actual behaviour: console logs "Array(0)" i.e. NO DATA!
I use firebase js library (version 4.13.1). Where am I wrong?

You need use val() method to return your data.
firebase.database().ref().once('value').then(function(snapshot){
console.log(snapshot.val());
})
Edit: Then your problem exist in another piece of code what you did not show.
Check this example: http://jsfiddle.net/zgfr91yd/
In console log:
And see my data:
You need a unique key for parent of each restaurant. You can use 'push' function in Firebase for that.

The snapshot argument that's being passed to your callback is an object of type DataSnapshot. You shouldn't assume that the JSON formatting string of a DataSnapshot contains just the data from that location in the database. As you can see from the API docs, there are a lot of methods functionality in a DataSnapshot.
What you what to do is call val() on that snapshot in order to extract a JavaScript value from it. Read the API docs to further understand what that object may contain.

Related

How to manually set timestamp for Splunk logs?

I have a database table full of logs, and I would like to upload that into Splunk by executing a query to get an array of logs, then iterating over it and running console.log() on each log. This almost works fine, however the timestamp that Splunk picks up for each log is based on whatever time I run the upload script.
Is there any way I can transform my log object so that Splunk will use the timestamps I provide? For example, one of my logs might look like:
{
id: 2910432221,
log_timestamp: 2019-08-07T19:04:03.000Z,
userId: 2331,
actionId: 45
}
Ideally, I would be able to run something like this, and have the value of splunk_timestamp appear in the Time column in the Splunk dashboard.
console.log({
id: 2910432221,
splunk_timestamp: 2019-08-07T19:04:03.000Z,
userId: 2331,
actionId: 45
})
Is anything like that possible?
Splunk will try to automatically identify the _time value by picking the first field it encounters matching TIME_FORMAT. You can override this in props.conf.
I believe your problem is in the json object: what you are passing to console.log is not a valid json object. Field names and values should be properly quoted.
{
"id": 2910432221,
"splunk_timestamp": "2019-08-07T19:04:03.000Z",
"userId": 2331,
"actionId": 45
}

How to access selected elements e.g. title, description, date from rss feed?

I am doing this for learning purpose. I have url to RSS feed that I'd like to work further. This feed contains too much information. I'm interested only in all "item", their "title", "description" and "pubDate". I am using "firebase deploy --only functions" and then checking url for deployment where I expect to see cleaned data. For some reason I am getting error in cloud functions logs: "TypeError: Cannot read property 'channel' of undefined
at cleanUp (/user_code/lib/index.js:19:29)"
I tried this with another URL which surprisingly worked: https://www.theguardian.com/uk/london/rss
Here is the URL to RSS feed I want to use:
https://polisen.se/aktuellt/rss/hela-landet/handelser-i-hela-landet/
Here is my cleanUp function:
function cleanUp(data) {
const items = []
const channel = data.rss.channel
channel.item.forEach(element => {
items.push({
title: element.title,
description: element.description,
date: element.pubDate
})
});
return items
}
I expect to see all items with children title, description and pubdate after deployment. Instead I get "Error: could not handle the request" and when I check my logs in google cloud functions I see:
"TypeError: Cannot read property 'channel' of undefined
at cleanUp (/user_code/lib/index.js:19:29)"
To do so I would recommend working with JSON objects. So what I did is to get the RSS feed and convert it to a JSON object. Then parse item by item and log the data you need.
I have did a little bit of coding my self. Follow the steps below to get an idea of my example and then modify the code according to your needs:
Create a new Google Cloud Function
Trigger: HTTP
Runtime: Node.js 8
Add dependencies in package.json:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"rss-to-json": "^1.0.4"
}
}
In index.js replace the code with my GitHub code example.
I have tested the code my self and it logged 200 items using the link you provided. Basically I take the RSS and convert it to JSON object. Then I iterate through all the items and log each item's property.

Firebase retrieves snapshot value as null

I use the .once( function in Firebase and am successfully retrieving the snapshot.key NAME of the directory. But this key has also a value.
When requesting snapshot.val() also gives out null despise it having a value.
The database looks like this:
stages {
stage1AVERYLONGHASH : "values i want to get"
stage2AVERYLONGHASH : "values i want to get"
}
My code
var ref = "stage1AVERYLONGHASH";
var branch = firebase.database().ref(ref).once('value').then(function(s){
console.log(s.key); // GIVES THE CORRECT ANSWER
console.log(s.val()); //GIVES null ... doesn't give me the "values i want.."
Am i doing it completely wrong?
I'm new to Firebase.
Please know that the database is structured like this intentionally.
I restricted read permission on "stages".
The game works as just requesting a hash from the database as a reference, to get contents.
I think you suppose to get the reference of the stage and then get the stage1 reference from him like :
.ref("stage").child("stage1")
I'm not sure about the actual syntax but I know that this is how I get my firebase data in android studio

Write an object containing an array of objects to a mongo database in Meteor

In my user collection, I have an object that contains an array of contacts.
The object definition is below.
How can this entire object, with the full array of contacts, be written to the user database in Meteor from the server, ideally in a single command?
I have spent considerable time reading the mongo docs and meteor docs, but can't get this to work.
I have also tried a large number of different commands and approaches using both the whole object and iterating through the component parts to try to achieve this, unsuccessfully. Here is an (unsuccessful) example that attempts to write the entire contacts object using $set:
Meteor.users.update({ _id: this.userId }, {$set: { 'Contacts': contacts}});
Thank you.
Object definition (this is a field within the user collection):
"Contacts" : {
"contactInfo" : [
{
"phoneMobile" : "1234567890",
"lastName" : "Johnny"
"firstName" : "Appleseed"
}
]
}
This update should absolutely work. What I suspect is happening is that you're not publishing the Contacts data back to the client because Meteor doesn't publish every key in the current user document automatically. So your update is working and saving data to mongo but you're not seeing it back on the client. You can check this by doing meteor mongo on the command line then inspecting the user document in question.
Try:
server:
Meteor.publish('me',function(){
if (this.userId) return Meteor.users.find(this.userId, { fields: { profile: 1, Contacts: 1 }});
this.ready();
});
client:
Meteor.subscribe('me');
The command above is correct. The issue is schema verification. Simple Schema was defeating the ability to write to the database while running 'in the background'. It doesn't produce an error, it just fails to produce the expected outcome.

Can't access a javascript Object via Postman's environment variables

I am new to postman API testing.
Recently I am using it's collection runner to test request iteration with data file(json).
The weird thing is if I try to access the environment variable that I set before, the console log always shows [object Object] instead of Object {}.
Here's my pre-request code on postman:
postman.setEnvironmentVariable("username", data.username);
postman.setEnvironmentVariable("password", data.password);
and the Tests code on postman:
var userData = {
username: environment.username,
password: environment.password
};
postman.setEnvironmentVariable("user", userData);
console.log(environment.user);
for(var key in environment.user){
console.log(key + ": "+ environment.user[key]);
}
Request URL: https://echo.getpostman.com/get?username={{username}}&password={{password}}
data.json:
[{
"username" : "Jon Snow",
"password" : "iloveGot123"
},{
"username" : "Akali",
"password" : "iWillKickyalla55"
},{
"username" : "Ricky Grimmes",
"password" : "rg0123455"
}]
Please help thank you.
According to the documentation:
postman.setEnvironmentVariable(variableName, variableValue): Sets an environment variable “variableName”, and assigns the string “variableValue” to it. You must have an environment selected for this method to work. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.
https://www.getpostman.com/docs/sandbox
So basically it's showing that because it wants a string only.
You can wrap the object with JSON.stringify() to convert it to a string and then use JSON.parse() when you want to turn it back into an object.

Categories