Meteor render data from embeded, nested collection - javascript

I've been trying to get data out of a nested collection without any luck, besides using the dot notation from the html nothing seems to work.
What I want basically is to soley fetch the data I need from a nested collection. I'm trying to build a file upload for images and audio files and then a simple way to use the files. I'm using the cfs:standard-packages and cfs:filesystem packages.
The code below shows a working example of what I don't want, eg fetching the whole file object and retrieving the data in the html. If I could use the dot notation in the mongo command somehow would be perfect. I also could settle for _each but I would prefer fetching just the data I need on each db call. As you can see I'm passing an id for the whole file object here. Uploads.find({_id:Session.get('getpic')}); BTW, the actual file is stored in a folder on my local server.
The collection:
{
"_id" : "DXFkudDGCdvLpPALP",
"original" : {
"name" : "drink.png",
"updatedAt" : ISODate("2015-04-30T07:14:56.000Z"),
"size" : 127944,
"type" : "image/png"
},
"uploadedAt" : ISODate("2015-07-11T21:53:32.526Z"),
"copies" : {
"uploads" : {
"name" : "drink.png",
"type" : "image/png",
"size" : 127944,
"key" : "uploads-DXFkudDGCdvLpPALP-drink.png",
"updatedAt" : ISODate("2015-07-11T21:53:32.000Z"),
"createdAt" : ISODate("2015-07-11T21:53:32.000Z")
}
}
}
HTML
<template name="renderImages">
{{#each showpic}}
<img width="300" height="300" src="/projectuploads/{{copies.uploads.key}}"/>
{{/each}}
Javascript:
Template.renderImages.helpers({
showpic: function() {
return Uploads.find({_id:Session.get('getpic')});
}
});

specify the returned fields in the find query like so
return Uploads.find({_id:Session.get('getpic')}, { fields: {'copies.uploads.key': 1} } );
but a word on that. here you query minimongo (on the client), which is in the browsercache so it's basically free. take care to publish only those fields to the client that you actually want there.

Related

Is it possible to move a particular JSON key to the top of the JSON using Javascript/VuejJS/Nodejs package?

I have a web application built using the Vuejs within that I am obtaining a JSON from the Java backend service which has a format something like this:
{
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
}
Before displaying the result to the user I would like to move the schema to the top so that it would be easy for the user to read and I want it to look something like this:
{
"schema" : "2.0",
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B"
}
As we can see only schema position has changed, the rest of the JSON as it is.
Please Note:
I am aware the JSON order does not matter but I am doing this for better readability purposes. If there is a way then it would be really useful for the reader to understand the JSON better.
I want to know if there is a direct way to do it rather than looping over the JSON, as my created JSON in the real application can be pretty large.
All I want to do is move the schema to the top of the JSON. The rest of the JSON can be as it is I do not want to make any modifications to it.
Is there a way to do this using vanilla Javascript or using some Nodejs library as I am using the Vuejs?
I would really appreciate it if there was a way to do it or is there any workaround for this.
A very simplistic approach could be to stringify a new object.
const myObject = {
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
};
console.log(
JSON.stringify({
schema: myObject.schema,
...myObject
}, null, 2)
);
My suggestion is almost the same as the accepted answer but without using JSON.stringify():
const myObject = {
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
};
const myReorderedObject = {
schema: '',
...myObject
};
console.log(myReorderedObject);

Convert JSON naming schema in Ember

I have an application where I retrieve JSON data from a server. Unfortunately the JSON data is formatted as such:
"Product_Group" : [
{
"Product_Group_ID" : "396xx",
"Product_Group_SEO_Copy" : "Not Included in JSON",
"Product_Group_Title" : "Some awesome Title",
"Products" : [
{
"On_Sale_Date" : "04\/28\/09 00:00:00.000",
"ISBN" : "97800617737xx",
"Title" : "A Disgraceful Affair",
//rest of the it follows
Ember throws errors when trying to use Capitalized JSON objects in a template. Specifically the Uncaught TypeError: Cannot read property 'unchain' of undefined error. This is related to this issue noted on the Github Ember site.
My question, how can I convert all the titles from the server into camelCase? I don't have access to the server to change things on that end, so it will have to be client side. I've looked into the DS.RESTSerializer but I don't understand it enough to know if that is what I should be using.
DS.RESTSerializer is exactly what you want to use. You can easily transform the keys of your attributes by using the keyForAttribute hook. You may also need to change the attribute name of your root node Product_Group to something like productGroup. You can do this with extractArray.
A serializer such as this might just do the trick:
App.ProductGroupSerializer = DS.RESTSerializer.extend({
extractArray: function(store, type, payload) {
return this._super(store, type, {
productGroup: payload['Product_Group']
});
},
keyForAttribute: function(attr) {
return Ember.String.camelize(attr);
}
});

MongoDB/NodeJS query to get data from dictionary

Hi in mongo DB I have a table "games" like this:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory_id" : "1",
"deviceOrientation_id" : "1",
"position" : "1"
}
and I have a few dictionaries (also simple collections in MongoDB) like "gameCategory" for example:
{
"_id" : 0,
"name" : "GAME_CATEGORY_NO_CATEGORY"
}
{
"_id" : 1,
"name" : "GAME_CATEGORY_POPULAR"
}
How to get data from collection "games" with fields from my dictionary like:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory" : GAME_CATEGORY_NO_CATEGORY, <---------------
"deviceOrientation_id" : "1",
"position" : "1"
}
Thanks. I'm using Node server for it.
What you are essentially asking for is a "JOIN" as in SQL. Your first point of reading should be that MongoDB does not do joins. The general concept here is "embedding" where the related information is actually contained within the document.
A good reading of the Data Modelling section of the official documentation can cover various points such as this and alternate approaches. But in most cases the data you wish to reference should just be part of the original document:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory" : "GAME_CATEGORY_POPULAR",
"deviceOrientation_id" : "1",
"position" : "1"
}
This is generally because the MongoDB concept of being "scalable" is that all operations only ever deal with one collection at a time and "JOINS" are not attempted.
There are options available under node.js such as Mongoose that allow you to .populate() items from another "related" collection. But the general problem here is that you cannot "query" on the "related" information. All this really implements is a "query behind the scenes" approach. So more than one query is actually executed. To find by "related" information the best approach is generally:
var catId = db.category.findOne({ "name": "GAME_CATEGORY_POPULAR" })._id;
db.category.find({ "gameCategory_id": catId })
As nothing will let you query the "game" collection by a value held in a foreign collection.
The idea of "embedding" and generally "duplicating" data might seem alien to those used to relational database concepts. But really your reason for applying a solution such as MongoDB should be that you realize certain "relational patterns" are not the "best fit" for your application.
If you have not looked at this in that way, then perhaps you should stick with the relational database approach and use those tools. At least until you "find" the actual shortcomings and realize why you need to "design" around that.
Unlearn what you have learned.

Access ShareJS metadata from DerbyJS

DerbyJS uses ShareJS for its data synchronization. ShareJS stores various metadata values, as you can see here:
{ "id" : "ABCDEFG12345", "_type" : "http://sharejs.org/types/JSONv0", "_v" : 3, "_m" : { "mtime" : 1403674645713, "ctime" : 1403674645618 }, "_id" : "ABCDEFG12345" }
The _type, _v and _m fields are automatically generated by ShareJS.
I know there is a similar question over at Is it possible to access m.mtime or m.ctime from share.js in derby.js? , but the only provided solution is that it is possible to use these fields for querying.
Is there any way to access these properties from the fetched object using Derby ?
No. These fields are striped in ShareJS db adapter level.
source

AngularJS $scope data across different pages

I'm getting to grips with AngularJS and I am working on an application just now where I have questions and answers.
The questions use an incrementing + and - button per item which updates the $scope
What I am wondering though, because I will have to access the values from the question side of the app in the answers, what would be the simplest way to get this across. I had thought of storing the $scope.questions into localstorage.
{
"uid" : 1,
"name": "Quiz",
"drinks" : [
{
"id" : 0,
"type" : "Footballs",
"image" : "http://placehold.it/280x300",
"amount" : ""
},
{
"id" : 1,
"type" : "Golf Balls",
"image" : "http://placehold.it/280x300",
"amount" : ""
}
]
}
The above is json which is fed into my page and then using ng-repeat it will display and then the amount keys get updated when the user has clicked either + or -.
I would like to somehow update this json to be accessable throughout the site too so that when the user/client has updated it they can view a separate page which shows them the answers.
Used localStorage to carry data from the $scope around
localStorage.setItem('data', angular.toJson($scope.data));

Categories

Resources