Ember Data belongsTo Association (JSON format?) - javascript

I have two models 'Author' and 'Publisher' (Rails), with a publisher hasOne author / author belongsTo publisher relationship.
I have the Ember models setup correctly -- JS Fiddle -- and the associations working when I manually push into the store. But only the publisher records are created when requesting /publishers index.
I've tried several types of JSON responses:
Publishers with author
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": 1
}
],
"author": {
"id": 1,
"name": "Test A 1",
"publisher": 1
}
}
Publishers with authors
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": 1
}
],
"authors": [{
"id": 1,
"name": "Test A 1",
"publisher": 1
}]
}
Publishers with author embedded
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": {
"id": 1
"name": "Test A 1"
}
}
]
}
Thanks for any help!

The ActiveModelAdapter/ActiveModelSerializer expects _id/_ids to be appended on relationships
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author_id": 1
}
],
"authors": [{
"id": 1,
"name": "Test A 1",
"publisher_id": 1
}]
}
http://jsfiddle.net/6Z2AL/1/

Adding a link to ember-data issue in case it helps anyone -- single object push payload

Related

Mongodb: How to group the result of a $lookup and map them to a list objects that contain a matching key?

I have classroom document which contain 'modules', the whole document looks like this:
{
"_id": "628a7ea21e2a666d7872efbf",
"name": "Test Class",
"owner": "60763491b98b9e186ef33137",
"schoolId": "607dff27c712219af1e65d83",
"description": "This is a test class.",
"roster": [],
"modules": [
{
"name": "Test Module 1",
"id": "62a7082d0bf84c43fdfe95ff",
"isPublished": false
},
{
"name": "Test Module 2",
"id": "62a72d6378ce044dca32e1a2",
"isPublished": false
}
]
}
I also have assignment documents as such:
{
"classroomId": "628a7ea21e2a666d7872efbf",
"moduleId": "62a7082d0bf84c43fdfe95ff",
"name": "Assignment 1",
"description": "Test description",
"created": 1655120822055,
"reading": null,
"questions": [],
"isPublished": true,
"_id": "62a723b6683ffc4b11940c7b"
}
My question is how could do aggregation such that if I want to do a lookup on the assignments for the classroom, I am able to group the assignment documents by moduleId and then add them as a field to the modules array. The final document would like this:
{
"_id": "628a7ea21e2a666d7872efbf",
"name": "Test Class",
"owner": "60763491b98b9e186ef33137",
"schoolId": "607dff27c712219af1e65d83",
"description": "This is a test class.",
"roster": [],
"modules": [
{
"name": "Test Module 1",
"id": "62a7082d0bf84c43fdfe95ff",
"isPublished": false,
"assignments": [
{
"_id": "62a708ab0bf84c43fdfe9600",
"classroomId": "628a7ea21e2a666d7872efbf",
"moduleId": "62a7082d0bf84c43fdfe95ff",
"name": "Assignment 1",
"description": "Test description",
"created": 1655113899629,
"due": null,
"settings": null,
"reading": null,
"questions": [],
"isPublished": true
},
{
"_id": "62a723b6683ffc4b11940c7b",
"classroomId": "628a7ea21e2a666d7872efbf",
"moduleId": "62a7082d0bf84c43fdfe95ff",
"name": "Assignment 1",
"description": "Test description",
"created": 1655120822055,
"due": null,
"settings": null,
"reading": null,
"questions": [],
"isPublished": true
}
]
},
{
"name": "Test Module 2",
"id": "62a72d6378ce044dca32e1a2",
"isPublished": false,
"assignments": [
]
}
]
}
Right now I just have the base lookup and obviously this just gets me a separate assignments field, without groups.
lookup = [
{
$lookup: {
from: "assignments",
localField: "modules.id",
foreignField: "moduleId",
as: "assignments"
}
}
];
$lookup
$set - Set modules field.
2.1. $map - Iterate the modules array and returns a new array.
2.1.1. $mergeObject - Merge current iterate module document with the document with assignments array from the result 2.1.1.1.
2.1.1.1. $filter - Filter the assignments array by matching moduleId.
$unset - Remove assignments field.
db.classroom.aggregate([
{
$lookup: {
from: "assignments",
localField: "modules.id",
foreignField: "moduleId",
as: "assignments"
}
},
{
$set: {
modules: {
$map: {
input: "$modules",
as: "module",
in: {
$mergeObjects: [
"$$module",
{
"assignments": {
$filter: {
input: "$assignments",
as: "asgn",
cond: {
$eq: [
"$$module.id",
"$$asgn.moduleId"
]
}
}
}
}
]
}
}
}
}
},
{
$unset: "assignments"
}
])
Sample Mongo Playground

AppendData function: Insert JSON Content into HTML

I am following this tutorial to learn how to insert Text from a JSON into an Website. I rebuilt the example on the website succesfully - however I struggle to use my own JSON, which has a slightly different structure.
The JSON from the tutorial looks like this:
[
{
"id": "1",
"firstName": "John",
"lastName": "Doe"
},
{
"id": "2",
"firstName": "Mary",
"lastName": "Peterson"
},
...
]
The array is loaded into an object called data and I can access each of its values using an index and the key of the value i.e. data[0].firstName would result in "John" (as described in the short tutorial).
Now, the JSON I actually want to use has the following structure:
{
"Category1": [
{
"name": "Lorem",
"description": "A description",
"product": "a product",
...
}
],
"Category2": [
{
"name": "Lorem1",
"description": "Another description",
"product": "another product",
...
},
{
"name": "Lorem2",
"description": "A third description",
"product": "a third product"
...
},
{
"name": "Lorem3",
"description": "And one last description",
"product": "last product",
....
}
],
"Category3": [
{
...
},
],
...
}
How can I access all of these values? I tried to treat the data object as a multidimensional array (i.e. data[0][1] without success.
I am using the following function to insert into the HTML:
function appendData(data) {
var mainContainer = document.getElementById("myData");
for (var i = 0; i < data.length; i++) {
var div = document.createElement("div");
div.innerHTML = 'Name: ' + data[i].firstName;
mainContainer.appendChild(div);
}
}
Also - as a follow-up question: Is there a way to iterate through the Categories at all?
Call by data.ObjectName[index].propertyName
let data = {
"Category1": [
{
"name": "Lorem",
"description": "A description",
"product": "a product",
},
]
}
console.log(data.Category1[0].name);
For Example:
let data = {
Category1: [
{
name: "Lorem",
description: "A description",
product: "a product",
},
],
Category2: [
{
name: "Lorem1",
description: "Another description",
product: "another product",
},
{
name: "Lorem2",
description: "A third description",
product: "a third product",
},
],
};
for (x in data) {
for (let i = 0, l = data[x].length; i < l; i++) {
console.log(data[x][i].name);
}
}
Well... You can declare your arrays as a "normal" two-dimensional array, like:
const data = [
[{ "name": "Lorem", "description": "A description", "product": "a product" }],
[
{ "name": "Lorem1", "description": "A description", "product": "another product" },
{ "name": "Lorem2", "description": "A description", "product": "a third product" }
]
];
console.log(data[0][0]);
In case when you can't change your JSON structure you can iterate using Object.entiries:
const data = {
Category1: [
{
name: "Lorem",
description: "A description",
product: "a product",
},
],
Category2: [
{
name: "Lorem1",
description: "Another description",
product: "another product",
},
{
name: "Lorem2",
description: "A third description",
product: "a third product",
},
],
};
Object.entries(data).forEach((entry) => {
const categoryName = entry[0];
const values = entry[1];
// Replace console.logs with your functions that create div's/insert text/etc
console.log('\n');
console.log(categoryName);
values.forEach((value) => {
console.log('===================');
console.log(value.name);
console.log(value.description);
console.log(value.product);
});
})

Axios post method refreshes the page

So I got this problem whenever I post something in my database on localhost with Axios, the page gets refreshed automatically. Searched the internet for the same problem, but didn't really find anything, everything was on the HTML form things and I don't really have a form on my page.
codepen: https://codepen.io/shotaivanidze/pen/PooLNPZ
Here is a JSON database
{
"tags": [
{
"title": "test 1",
"id": 1
},
{
"title": "test 2",
"id": 2
},
{
"title": "test 3",
"id": 3
},
{
"title": "test 4",
"id": 4
},
{
"title": "test 5",
"id": 5
},
{
"title": "test 6",
"id": 6
},
{
"title": "test 7",
"id": 7
}
]
}

Restructuring JSON in JavaScript

I have this JSON which I'm parsing using NodeJS and it needs to be restructured into the second JSON which I've added below.
In the first JSON, the rows object has two pages objects (any number of pages objects can be present) which contains all the same keys and values with the exception of values and display keys.
{
"pages": [
{
"label": "SomeLabel",
"name": "Some",
"sections": [
{
"type": "Repeat",
"label": "Label 1",
"name": "Name 1",
"rows": [
{
"pages": [
{
"label": "Label 1",
"name": "Name 1",
"sections": [
{
"type": "Flow",
"label": "Label 2",
"name": "Name 2",
"answers": [
{
"label": "Question Label",
"question": "Question",
"values": [
"Value A"
],
"valuesMetadata": [
{
"display": "Display A",
"row": {
"columns": []
}
}
]
}
]
}
]
}
]
},
{
"pages": [
{
"label": "Label 1",
"name": "Name 1",
"sections": [
{
"type": "Flow",
"label": "Label 2",
"name": "Name 2",
"answers": [
{
"label": "Question Label",
"question": "Question",
"values": [
"Value B"
],
"valuesMetadata": [
{
"display": "Display B",
"row": {
"columns": []
}
}
]
}
]
}
]
}
]
}
],
"footer": null
}
]
}
]
}
In the second JSON the rows object has a single pages object, inside of which the values and display keys have multiple values (the non-common values).
{
"pages": [
{
"label": "SomeLabel",
"name": "Some",
"sections": [
{
"type": "Repeat",
"label": "Label 1",
"name": "Name 1",
"rows": [
{
"pages": [
{
"label": "Label 1",
"name": "Name 1",
"sections": [
{
"type": "Flow",
"label": "Label 2",
"name": "Name 2",
"answers": [
{
"label": "Question Label",
"question": "Question",
"values": [
"Value A",
"Value B"
],
"valuesMetadata": [
{
"display": [
"Display A",
"Display B"
],
"row": {
"columns": []
}
}
]
}
]
}
]
}
]
}
],
"footer": null
}
]
}
]
}
So, I want to know the fast and easy steps to do this. Please let me know the process and methods to solve this.
Thanks
If I understand you correctly, you want to combine all pages in a single page that holds all information.
This can be achieved using the Array.reduce function. reduce takes an array and combines all elements to a single value using a function (provided by you) to combine the first two elements until only one is left (i.e. 1 * 2 => new1; new1 * 3 => new2 where * represents your function).
Your problem would look something like this:
rows[0].pages = rows[0].pages.reduce((currentElement, currentState) => {
if (!currentState) { // first iteration return first element but make sure display is an array
currentElement.sections[0].answers[0].valuesMetadata[0].display =
[currentElement.sections[0].answers[0].valuesMetadata[0].display];
return currentElement;
}
// add values of current element to arrays in current state
currentState.sections[0].answers[0].values
.concat(currentElement.sections[0].answers[0].values);
currentState.sections[0].answers[0].valuesMetadata[0].display
.concat(currentElement.sections[0].answers[0].valuesMetadata[0].display);
return currentState;
});
currentElement is the object of the array that is currently reduced, currentState is the intermediate result of the reduction.
PS:
The object looks like you are way too many arrays where you would not need them. The given code snippet works only for the first element in each array (hence the [0]s. If you really do have multiple values in each array you would have to iterate over all of those accordingly.

Check for Parent Child node in JSON using angular JS

I am looking for JSON parsing reference, from where I can jump to question and check for Child question based on Yes section. I didn't find anything related to check for child node check in JSON. Angular is my base framework.
some use cases :
on load show Root question,
On selection show next questions which is child of root then go one
jump to number of questions from tree.
treeObj={
"Root_Element": {
"id": "myTree",
"dt": {
"choice": {
"id": '0',
"title": "Which color",
"description": "Choose color ?",
"choice": [
{
"id": 1,
"title": "Yellow",
"description": "Yellow ? ,
"choice": [
{
"id": 5,
"title": "Dark Yellow",
"description": "Dark Yellow ?
},
{
"id": 4,
"title": "Light Yellow",
"description": "Light Yellow ?
}
]
},
{
"id": 2,
"title": "Red",
"description": "Red ?"
},
{
"id": 3,
"title": "Green",
"description": "Green ?
}
]
}
}
}
}
If the number of levels in the JSON object is fixed and if it does not grow dynamically, you can use the ES6 destructuring to read the data from the nested JSON. Below is an example
var metadata = {
title: "Scratchpad",
translations: [
{
locale: "de",
localization_tags: [ ],
last_edit: "2014-04-14T08:43:37",
url: "/de/docs/Tools/Scratchpad",
title: "JavaScript-Umgebung"
}
],
url: "/en-US/docs/Tools/Scratchpad"
};
var { title: englishTitle, translations: [{ title: localeTitle }] } = metadata;
console.log(englishTitle); // "Scratchpad"
console.log(localeTitle); // "JavaScript-Umgebung"

Categories

Resources