i have a POST request with some Data, i post with Postman , Data like this , But i have some problem is when i console.log(newLesson) i cannot get value in [Array] , but when i try console.log(req.body.quizs) its can !!! Can some one explain to me why ? And how can i get values from that ? Many thanks 😊
"name":"This is name",
"videoId" : "this is videoId",
"level" : "beginner",
"script" : "this is Script",
"quizs" : [
{
"question" : "This is question 1",
"answer" : [
"Answer 1",
"Answer 2",
"Answer 3"
],
"correct_Answer" : "Answer 3"
},
{
"question" : "This is question 2",
"answer" : [
"Answer 1",
"Answer 2",
"Answer 3"
],
"correct_Answer" : "Answer 1"
}
]
}
And this is my backend when received
module.exports.addNewLesson = function(req,res){
let newLesson = {
name : req.body.name,
videoId : req.body.videoId ,
level : req.body.level,
script : req.body.script,
quizs : req.body.quizs
};
console.log(newLesson);
}
And this is what i got
{
name: 'This is name',
videoId: 'this is videoId',
level: 'beginner',
script: 'this is Script',
quizs: [
{
question: 'This is question 1',
answer: [Array],
correct_Answer: 'Answer 3'
},
{
question: 'This is question 2',
answer: [Array],
correct_Answer: 'Answer 1'
}
]
}
How to can i get answer : [Array] ??? Thanks
Try stringifying the output with formatting:
console.log(JSON.stringify(newLesson, null, 4)); // 4 is the number of whitespaces used for formatting
Related
I am new to React and am trying to figure out how to return a particular objects entry by it's key.
So far, I have an object as such:
const questions = [
{
id: '1',
section: 's1',
answers: [
"answer a",
"answer b",
"answer c",
"answer d",
]
},
{
id: '2',
title: 'Question 2',
answers: [
"answer a",
"answer b",
"answer c",
"answer d",
]
},
//etc
which I am currently iterating through and using parts of as props in a component, eg:
return (
<div>
{questions.map((question) => (
<Question
key={question.id}
questionNum={question.id}
title={question.title}
answers={question.answers}
/>
))}
</div>
);
This works fine and as expected.
But now I need to modify this so it only returns the values of 1 given particular key in the object.
I've been searching and experimenting with .get() but to be honest I'm really stumped with how to do this.
Would anyone know the best way to approach this?
You can filter the questions by a particular key of the object first and then do the map. Let's say id with value 1.
return (
<div>
{questions
.filter(({ id }) => id === "1")
.map(question => (
<Question
key={question.id}
questionNum={question.id}
title={question.title}
answers={question.answers}
/>
))}
</div>
)
If I got it right, I guess you can do the same using map itself
Assume you have the below array
const questions = [
{
id: '1',
section: 's1',
answers: [
"answer a",
"answer b",
"answer c",
"answer d",
]
},
{
id: '2',
title: 'Question 2',
answers: [
"answer a",
"answer b",
"answer c",
"answer d",
]
}]
and now you wanna just pull out the answers key alone then you can do something like
const result = questions.map((question) => {answers: question.answers}); // where the specific key here is answers
The filter on UI allows a user to search using the IN criterion. The search string will then become the property of the condition Object.
The search string will look like "1234,5436,8765" and then the condition Object will be like ```
condition: {
field: "Id",
operator: "in",
value: "1234,5436,8765"
}
Now as this is going to be IN filter, so whar criterion should I use so as to make the value search like IN criterion from the results Array.
e.g. for a like filter, I will set my value property like this %1234% so as to search this in the results Array.
Your question isn't clear so try to edit it to make it clearer.
I made a huge assumption here in my answer (as it is not clear from the question):
You could do it with a switch statement and then your various filter methods for each case.
You could do it more robustly, but until the question is clearer, it doesn't make much sense to build this solution out.
const myArrOfObs = [
{Id: 1234, title: "some title 1", other: "some other val 1"},
{Id: 2468, title: "some title 2", other: "some other val 2"},
{Id: 8240, title: "some title 3", other: "some other val 3"},
{Id: 9371, title: "some title 4", other: "some other val 4"},
{Id: 5436, title: "some title 5", other: "some other val 5"},
{Id: 8365, title: "some title 6", other: "some other val 6"},
{Id: 8765, title: "some title 7", other: "some other val 7"},
{Id: 3946, title: "some title 8", other: "some other val 8"}
];
const condition = {
field: "Id",
operator: "in",
value: "1234,5436,8765"
};
function filterArr(arr, cond) {
switch (cond.operator){
case "in":
const valueArr = cond.value.split(',');
return arr.filter(item => valueArr.includes(item[cond.field].toString()));
//case "like":
//return arr.filter( ... );
default:
return null;
}
}
const myFilteredArr = filterArr(myArrOfObs, condition);
console.log(myFilteredArr);
I have 2 same dynatrees in the same page as shown in this js fiddle http://jsfiddle.net/37ppf/3/.
$("#tree1").dynatree({
onSelect : function(select,node){},
checkbox: true,
children: [
{title: "Item 1",key:"1"},
{title: "Folder 2", isFolder: true, key:"2",
children: [
{title: "Sub-item 2.1", },
{title: "Sub-item 2.2", }
]
},
{title: "Item 3",key:"5"}
]
});
$("#tree2").dynatree({
onSelect : function(select,node){},
checkbox: true,
children: [
{title: "Item 1",key:"1"},
{title: "Folder 2", isFolder: true, key:"2",
children: [
{title: "Sub-item 2.1", },
{title: "Sub-item 2.2", }
]
},
{title: "Item 3",key:"5"}
]
});
<div id="tree1"> </div>
<div id="tree2"> </div>
Now When a user selects any node in any of the dynatree I want to get the div id in which the tree from which the user has selected the node is loaded.i.e if user selects from first tree I want to get output as tree1(div id) and if a node is selected from second tree i want to get tree2. Is this possible. I tried
$(this).closest(".dynatree-container").parent("div").attr("id")
But its coming undefined.
It feels super hacky, but you can do it this way:
onSelect : function(select,node){
alert(node.tree.$tree[0].id);
},
node.tree.$tree[0] will return the javascript object of its parent tree.
See the working code at
JSFiddle
Try this
$(document).on('click','span',function(){
console.log($(this).parents('div').attr('id'))
})
DEMO
I need to remove one of the 'answers' objects nested in the Doc below. I have the text of the answer I'm looking for. I don't have the index of the question OR the answer that I need to drill down into the arrays.
For example, I know that the text of the question I'm trying to drill down into is "This is a question." and the answer I want to delete is "Answer One".
How would you go about doing that?
Here's the sample MongoDB Doc:
(Quizzes have Questions; Questions have Answers)
{
name: "Sample Quiz",
categories: [
{ name: "testcategory1", description: "this is a test category" }
,{ name: "categoryTWO", description: "the second category" }
],
questions: [
{ text: "This is a question."
,answers: [
{text: "Answer One", affected_categories: "testcategory1"}
,{text: "Answer Two", affected_categories: "testcategory1"}
,{text: "Answer Three", affected_categories: "categoryTWO"}
]
}
,{ text: "This is the second question."
,answers: [
{text: "Mepho One", affected_categories: "testcategory1"}
,{text: "Answer Toodlydoo", affected_categories: "testcategory1"}
,{text: "Lehmen Sumtin", affected_categories: "categoryTWO"}
]
}
],
}
When I was deleting an item that was nested a single level down (in this case, a question), I was able to do it with a query like:
Quizzes.update(
{ _id: quizID, 'questions.text': questionText },
{ $pull: { questions: {text: questionText }}}
);
(as described here: http://docs.mongodb.org/manual/core/update/#Updating-ModifierOperations , in the section titled "Update an Element without Specifying Its Position")
I tried expanding that to something like:
Quizzes.update(
{ _id: quizID, 'answers.text': answerText },
{ $pull: { questions: {text: questionText {answers: {text: answerText }}}}}
);
but didn't have any luck.
Any ideas would be greatly appreciated.
Use the positional operator in combination with $pull condition:
> db.quizzes.update(
{_id:<quizID>, "questions.text":"This is the second question."},
{$pull:{ "questions.$.answers":{"text":"Answer Toodlydoo"}}}
);
The above works to remove the second answer from the second question.
I have a data object and inside that I have data object with json data inside. Here is how is looks like. the name of the file is data.js
var data =
[
{
title: 'this is title one',
commit: 'this is commit'
},
{
title: 'this is title two',
commit: 'this is commit'
},
{
title: 'this is title three',
commit: 'this is commit'
},
{
title: 'this is title four',
commit: 'this is commit'
},
{
title: 'this is title five',
commit: 'this is commit'
},
{
title: 'this is title six',
commit: 'this is commit'
},
{
title: 'this is title seven',
commit: 'this is commit'
}
]
here is my code
for (var i = 0; i < data.length; i++) {
$(container).load(view, function () {
console.log(data[i].title);
li.append('<img src="../images/profile.png" alt="Profile photo" />');
li.append('<span class="commit">' + data[i].title + '</span>');
li.append('<p>' + data.commit + '</p>');
li.append('<section class="clear"></section>');
$('#home ul').append(li);
});
}
Now I am getting this error in my console
Cannot read property 'title' of undefined
above error is on this line
li.append('<span class="commit">' + data[i].title + '</span>');
how to fix this?
Well, data.js is not really json.
First switch from load() to getJSON()
$.getJSON(view, function(data) { })
next remove the line
var data =
just have the json contents. jQuery will assign JSON to argument data.
Your json array contains errors.
You can validate your json here http://json.parser.online.fr/
Must be so:
{
"title": ["this is title one", "this is title two", "this is title three", "this is title four", "this is title five", "this is title six", "this is title seven" ]
,
"commit": [ "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit" ]
}