Issue with i18n parsing - javascript

I am getting JSON fro backend, which I need to parse on UI.
For all the keys from the JSON, I have to translate them and show on UI.
Eg:
i18n.t('key') will give me translated value.
But for some keys like 'name', 'date'
Eg:
i18n.t('name')
translation is giving following output
"key 'translation:name (en-US)' returned a object instead of string."
Could you please help me how to deal with this scenerio.

If you have for example following JSON from your service
{
"i18n": {
"name": "translation1",
"name2": "translation2"
}
}
You can just use it as following
var mytranslation = getTranslationsFromService();
console.log(mytranslation.i18n.name) //result: translation1
console.log(mytranslation.i18n.name2) //result: translation2
var getTranslationsFromService = function() {
//Get result from service, where the result looks like the JSON above.
}
I hope I could help.
Kind regards.

Related

I don't get the values from my firebase database

I am trying to retrieve data from my database and my code doesn't return the desired content. It's structure is this:
And this is my code:
firebase.database().ref('proiecte/').once('value').then(function(snapshot){
console.log(snapshot.val().nume)
});
I would expect for my console to return "ROSE" but it returns "undefined". Maybe it helps so I'm mentioning this too: if I run console.log(snapshot.val()) it returns this:
{…}
1544696773350: Object { descriere: "Proiectul este dedicat tinerilor!", nume: "ROSE" }
<prototype>: Object { … }
So, I'm sure that the database is correctly made just that I'm not using the right format for retrieving data. Can anyone tell me where I am wrong please?
To solve this issue, try the following:
firebase.database().ref('proiecte/').once('value').then(function(snapshot){
snapshot.forEach(function(childSnapshot){
let name = childSnapshot.val().nume;
let desc = childSnapshot.val().descriere;
});
});
Here your snapshot is at node proiecte, then you loop using forEach and retrieve the data that's under the random id, so you will be able to retrieve nume and descriere.
I managed to solve this issue!
What I did was to replace ".once('value').then(function(snapshot)" with ".on('child_added', function(snapshot)".

object has no method push in node js

I am trying to append the user details from the registration form to the json file so that the user-details can be used for authentication. the problem is i am not able append to the json file in correct format.The code i have tried so far is,
var filename= "./user_login.json";
var contents = fs.readFileSync(filename);
var jsonContent = JSON.parse(contents);
//sample data
var data =[
{
"try" : "till success"
}
];
jsonContent.push(data);
fs.writeFileSync(filename,jsonContent);
I have tried different methods that i found by googling and nothing worked so far. I want the data to be stored in correct format. Most of the times i got this error like object has no push function. So what is the alternative to that?
The correct format i am looking for is ,
[
user1-details : {
//user1 details
},
user2-deatils : {
}//So on
]
Object has no push function, arrays do. Your json is invalid too, it should be an array:
[ // here
{
//user1 details
},
{
//So on
}
] // and here
Now, you can use push(). However, data is an array, if you want an array of objects in your json file, it should be a simple object:
var data = {
"try" : "till success"
};
You also have to stringify the object before writing it back to the file:
fs.writeFileSync(filename, JSON.stringify(jsonContent));
You should consider using something like node-json-db, it will take care of reading/writing the file(s) and it gives you helper functions (save(), push()...).

Json stringify then parse from URL

I seem to be having an issue in stringify'ing then pasing from a url object
I simply stringify my object and set the location (with angulars $location) like so
currentUrl = {"module1" : {"is" : true} }
$location.search(JSON.stringify(currentUrl));
So this parses to the url just fine, however when I try to grab it from the url i get this back
console.log($location.search());
---
Object {{"module1":{"is":true}}: true}
How do I parse this back into an object so I can use it? If I do
JSON.parse($location.search());
I get a syntax error. I maybe because of how search returns the object? I am a bit confused here, could use some help. Thanks!
So I put it in the url with
$location.search(JSON.stringify(currentUrl));
What are the steps I need to take to get it back into this form :
{"module1" : {"is" : true} }
Edit -
It just appears it's setting the json object as the key in the location like
{ "mystrigifiedobject": true }
Edit2 :
based off the first edit, I was able to solve it (assming it's set in the locations object key) like so :
currentUrl = $location.search();
currentUrl = JSON.parse(Object.keys(currentUrl));
console.log(currentUrl);
This just feels a little weird though, am I doing something wrong here?
$location.search() returns the parsed search items from the url path as an object. This means this kind of url:
?a=b&c=d
will result in this object:
{ a: 'b', c: 'd' }
When you call this function:
currentUrl = {"module1" : {"is" : true} }
$location.search(JSON.stringify(currentUrl));
your path will look like this:
?%7B%22module1%22:%7B%22is%22:true%7D%7D
and the parsed object returned from $location.search will look like this:
{{"module1":{"is":true}}: true}
not that this is an object with one entry and the key is your JSON
So what you need to do in order to get your object back is this:
var parsedObject = $location.search();
var yourObject = JSON.parse(Object.keys(parsedObject)[0]);
see this jsfiddle: http://jsfiddle.net/HB7LU/11633/
But please note: you should encode your string when putting it in a url:
$location.search(encodeURIComponent(JSON.stringify(currentUrl)));
$routeParams provides access to both routing template values and query string values (as a string).
function ctrl($routeParams) {
var yourValueAsString = $routeParams.yourKey;
}
function ctrl2($location) {
$location.search('yourKey', JSON.stringify(...));
}
A better alternative would be to switch to using the UI Router which deals with this better.

How to use Array attributes in sails.js model

Hi i am a new bee to sails and trying to get a model api that finally gives output as follows
[
{
"icon" : [
{"name":"ico1", "ico_typ":"fb", "ico_content_URL":"someLocation"},
{"name":"ico2", "ico_typ":"tw", "ico_content_URL":"someLocation"},
{...}
]
"createdAt":
"updatedAt":
}
]
I thought i can achieve this by passing the icon attribute as an Array but the problem is it passes the whole Array as string when i load it in REST CLIENT also i could not use the validation for values inside Array like without ico_type and URL the data should not be loaded into database. So, any suggestion about use of the 'array' where i'm wrong is very appreciated, thanks so much!
Sails_v0.11.0
MongoDB_3.0.1
In your model define a method
toJSON: function () {
var obj = this.toObject();
//say your obj.icon returns something like `'[{"name":"ico1","ico_typ":"fb","ico_content_URL":"someLocation"},{"name":"ico2","ico_typ":"tw","ico_content_URL":"someLocation"}]'`
obj.icon = JSON.parse(obj.icon)
return obj;
},
I think the model WaterLine gave you is already an JSON format, all you need to do is to use the right way to response it.
res.json(model);

SQLike - understanding the basics

I'm trying to use the query engine
SQLike and am struggling with the basic concept.
The JSON I'm using as my data source comes from my PHP code, like so:
var placesJSON=<? echo json_encode($arrPlaces) ?>;
Here's a sample JSON:
var placesJSON=[{"id":"100","name":"Martinique","type":"CTRY"},{"id":"101","name":"Mauritania","type":"CTRY"},{"id":"102","name":"Mauritius","type":"CTRY"},{"id":"103","name":"Mexico","type":"CTRY"},{"id":"799","name":"Northern Mexico","type":"SUBCTRY"},{"id":"800","name":"Southern Mexico","type":"SUBCTRY"},{"id":"951","name":"Central Mexico","type":"SUBCTRY"},{"id":"104","name":"Micronesia, Federated States","type":"CTRY"},{"id":"105","name":"Moldova","type":"CTRY"}];
I understand (via this reference) that I first need to unpack my JSON like so:
var placesData = SQLike.q(
{
Unpack: placesJSON,
Columns: ['id','name','type']
}
)
And the next step would be to query the results like so:
var selectedPlaces = SQLike.q(
{
Select: ['*'],
From: placesData,
OrderBy: ['name','|desc|']
}
Lastly, to display the results in the browser I should use something like:
document.getElementById("myDiv").innerHTML=selectedPlaces[0].name
This doesn't work. The error I get is: selectedPlaces[0].name is undefined.
I'm pretty sure I'm missing out on something very simple. Any hints?
"Unpack" converts an array of arrays, like [["John", "Peterson", 38, 28000], ["Vicki", "Smith", 43, 89000]] into an array of objects. Since your Json is already in this format, there's no need to unpack it.

Categories

Resources