Firstly, I'm very new to nodeJS... like day 5. So bear with me.
I'm building an FAQ app to get into it and I have written a controller and a model which is returning this JSON file:
[
{
faq_id: 1,
faq_question: "This is a question?",
faq_answer: "And a lovely answer",
tab_title: "Tab title here",
tab_id: 1
},
{
faq_id: 2,
faq_question: "Another question",
faq_answer: "With an answer",
tab_title: "Tab title",
tab_id: 2
}
]
}
The requirements are that all the questions are categorised into tabs on the FE. So two tables, one for tabs and one for faqs.
Where each question has an appropriate tab_id associated with it to make the relationship.
What I need to do is create top level nesting in the JSON file to be all the tabs, then iterate over the questions and match the faq_tab_id with the tab_id and nest the question into the appropriate tab.
I have started with looking into using an UnderscoreJS map?
Not sure how much sense it makes or if any more code would be required, but any advice would be welcome. I'm not clued up on the terminology yet so I struggled to find an already asked question to do something like this.
Thanks!
Depending on what you intend to do, Underscore.js groupBy function may be enough:
// This will group the objects by their 'tab_id' property
var grouped = _.groupBy(myData, 'tab_id');
Related
I am having difficulty accessing all the data returned by my forms in my post function. I notice a significant discrepancy between what is displayed when I print request.POST vs. when my code accesses this data. Hopefully someone can explain this to me.
Output of print(request.POST):
print(request.POST)
<QueryDict: {'csrfmiddlewaretoken': ['AXMPO...'],
'start_date': ['2019-03-01'], 'end_date': ['2019-03-26'],
'reports': ['4', '1']}>
In order to examine the data my code is dealing with I used the json module to view the data. The behavior of my code during debugging conforms to this representation:
json.dumps(request.POST)
'{"csrfmiddlewaretoken": "AXMPO...",
"start_date": "2019-03-01", "end_date": "2019-03-26",
"reports": "1"}'
It all looks pretty similar until you see the "reports" value. The user selects these reports via an MultipleSelect widget on my form and my code is iterating through the id numbers provided. However, no matter how many reports I select I only get one ID. If anyone can explain why this is happening I would sincerely appreciate it.
Turns out this is a really old school issue. I could wish this was more prominent in the documentation though. The explanation by Simon Willson is below:
"""
This is a feature, not a bug. If you want a list of values for a key, use the following:
values = request.POST.getlist('key')
The reasoning behind this is that an API method should consistently return either a string or a list, but never both. The common case in web applications is for a form key to be associated with a single value, so that's what the [] syntax does. getlist() is there for the occasions (like yours) when you intend to use a key multiple times for a single value.
""" - Simon Willson, 13 years ago.
I am interested in the getting a list of the question based on a tag or a search query. I will give you an example.
So If I use the search keyword as "ipv4", it should give me a big list of questions related to ipv4. All I want to do is get the questions (title) as a list or an array so that I can do some processing on it.
Stackexchange offers https://api.stackexchange.com/docs/advanced-search endpoint.
So for example, get on
https://api.stackexchange.com/search/advanced?site=stackoverflow.com&q=firebase
would return you something like this:
This is simplest example but as you will find in the docs, there are numerous parameters based on which search can be performed. Some of them are:
accepted - true to return only questions with accepted answers, false to return only those without. Omit to elide constraint.
answers - the minimum number of answers returned questions must have.
body - text which must appear in returned questions' bodies.
tagged - a semicolon delimited list of tags, of which at least one will be present on all returned questions.
title - text which must appear in returned questions' titles.
user - the id of the user who must own the questions returned.
...
Hope this helps!
Cheers!
You can get this information utilizing the questions/ route. In this call, you will pass the tag(s) you are interested in to the tagged parameter (separated by a semicolon (;)).
To constrain questions returned to those with a set of tags, use the tagged parameter with a semi-colon delimited list of tags. This is an and constraint, passing tagged=c;java will return only those questions with both tags. As such, passing more than 5 tags will always return zero results.
For your specific question (searching for ipv4), you can utilize this as a starting point:
http://api.stackexchange.com/docs/questions#order=desc&sort=activity&tagged=ipv4&filter=!BHMIbze0EPheMk572h0ktETsgnphhU&site=stackoverflow&run=true
The filter is optional, but I've stripped out some of the default fields to present a smaller example. The link above returns entries that look like this:
"items": [
{
"tags": [
"ruby-on-rails",
"ipv4",
"geokit"
],
"link": "http://stackoverflow.com/questions/29460004/rails-geokit-incorrectly-converting-ipv4-address-to-latitude-and-longitude",
"title": "Rails: Geokit incorrectly converting IPv4 address to latitude and longitude"
},
{
"tags": [
"networking",
"ip",
"ipv4",
"maxmind",
"cidr"
],
"link": "http://stackoverflow.com/questions/28358851/merging-of-multiple-ipv4-address-blocks-on-the-basis-of-their-country-region",
"title": "merging of multiple IPv4 address blocks on the basis of their country region"
},
...
}
I was struggling with this question on how to get relevant results from the API as even after giving relevant questions it was returning irrelevant answers then I flipped the sort option from 'activity' to 'relevance' and voila it was working similarly as the stack overflow search system and was returning same articles.
Use the advanced search option and put your question in the 'q' parameter and change the 'sort' parameter from 'activity' to 'relevance'. To search based on tags put the tags in the 'tagged' parameter, each tag separated by a semicolon.
How to get the titles:
Now in the JSON response, all the matching objects are inside an array in the 'items' object. each item in the array is a question and each item has a 'title' parameter which holds the title of the question.
Example:
If I were to search for the question 'how to center a div in HTML, the link will be like this
https://api.stackexchange.com/2.3/search/advanced?order=desc&sort=relevance&q=how%20to%20center%20a%20div%20in%20html&site=stackoverflow
I have a rather big Json object that I am receiving via $http that I want to share between 2 controllers. To give you an idea - both controllers share the parent most array (marked as levels), and then within the levels there are key:values with nested arrays ( 2 of them) and each controller uses and edits them separately. I am trying to consolidate this as much as possible so I thought if i could control 1 json between 2 controllers this would work.
I want to be able to edits each sub array and push back to the server as a whole, but before I attempt this step I would like to be sure something like this is even possible. I have tried using a factory and service to no avail. I am slo curious if I can do this and I bring in the array - is it 2 way binded? The first controller should be able to add more levels, and the second one can add to them in a differen't way the first one can't. If I were to bring in the json and share it between controllers and added a level on the first one, would it show on the second?
I have been banging my head against this for a while now, so any insight or direction would be very appreciated. Thanks!!
I took a stab at what the json might look like (forgive me if the syntax is off)-
{
"level1": {
"form": {
"form1": "sample1",
"form2": "sample2",
"form3": 500,
"form4": 500
}
"sublevels": {
"subL": ["1", "2", "3"],
"help1": "main_window",
"help2": 500
}},
"level2": {
"form": {
"form1": "sample1",
"form2": "sample2",
"form3": 500,
"form4": 500
}
"sublevels": {
"subL": ["1", "2", "3"],
"help1": "main_window",
"help2": 500
}
}
}
so both controllers would build off the levels, something like "level in levels" ng-repeat type scenario, but controller 1 would use the "Form" key, and controller 2 would use the "subevels" keys. they would be able to edit those sections and post the whole sting back to the server for a save. I don't have much else because the factories/services I Tried to setup didn't seem to work at all. Thanks!
you can put it on service, but a simpler way is use rootscope, or create a parent controller common for both of your controllers
Hi so I'm new to CouchDB looks great so far, but really struggling with what must be simple to do!
I have documents structured as:
{
"_id" : "245431e914ce42e6b2fc6e09cb00184d",
"_rev": "3-2a69f0325962b93c149204aa3b1fa683",
"type": "student",
"studentID": "12345678",
"Name": "Test",
"group: "A"
}
And would like to access them them with queries such as http://couchIP/student?group=A or something like that. Are Views what I need here? I don't understand how to take the parameter from the query in the Map functions in Views. example:
function(doc,req) {
if(req.group==='A'){
emit(doc.id, doc.name);
}
}
Is my understanding of how Couch is working wrong or what's my problem here? Thanks in advance, I'm sure this is Couch 101
Already read through http://guide.couchdb.org/ but it didn't really answer the question!
You need views to achieve the desired results.
Define the following map function inside a view of a design document. ( let's name the view "byGroup" and assume this lives in a design document named "_design/students" )
function(doc) {
if(doc.group){
emit(doc.group,null);
}
}
Results can be obtained from the following url
http://couchIP:5984/dbname/_design/students/_view/byGroup?startkey="A"&endkey="A"&include_docs=true
To have friendly url couchdb also provides url rewriting options.
You need to some further reading about views and the relevance that they return key/pair values.
It's not clear what you want to return from the view so I'll guess. If you want to return the whole document you'd create a view like:
function (doc) { emit(doc.group, doc) };
This will emit the group name as a key which you can lookup against, the whole doc will be returned as the value when you look it up.
If you want to just have access to the names of those users you want to do something like:
function (doc) { emit(doc.group, doc.name) };
Your question arises from a misconception about what a view does. Views use map/reduce to generate a representation of your data. You have no control of the output of your view in your query because the view is updated according to changes in your DB documents only.
Using a list is also not a good option. It may seem that you can use knowledge of your request in your list to generate a different output depending on the query parameters but this is wrong because couchdb uses ETags for caching and this means that most times you will get the same answer regardless of your list parameters since the underlying documents won't have changed. There is a trick though to fool couchdb in this case and this implies using two different alternating users but I wouldn't even try this way because surely there are easier ways to achieve your objectives and you can probably solve your problem using group as a key in your map function.
I've recently added the Sphider crawler to my site in order to add search functionality. But the default search.php that comes with the distribution of Sphider that I downloaded is too plain and doesn't integrate well with the rest of my site. I have a little navigation bar at the top of the site which has a search box in it, and I'd like to be able to access Sphider's search results through that search field using Ajax. To do this, I figure I need to get Sphider to return its results in JSON format.
The way I did that is I used a "theme" that outputs JSON (Sphider supposts "theming" its output). I found that theme on this thread on Sphider's site. It seems to work, but more strict JSON parsers will not parse it. Here's some example JSON output:
{"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ", "results":[ { "idented":"false", "num":"1", "weight":"[100.00%]", "link":"http://www.avtainsys.com/articles/Triple_Contraints", "title":"Triple Contraints", "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or", "link2":"http://www.avtainsys.com/articles/Triple_Contraints", "size":"3.3kb" }, { "num":"-1" } ], "other_pages":[ { "title":"1", "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=", "active":"true" }, ] }
The issue is that there is a trailing comma near the end. According to this, "trailing commas are not allowed" when using PHP's json_decode() function. This JSON also failed to parse using this online formatter. But when I took the comma out, it worked and I got this better-formatted JSON:
{
"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ",
"results":[
{
"idented":"false",
"num":"1",
"weight":"[100.00%]",
"link":"http://www.avtainsys.com/articles/Triple_Contraints",
"title":"Triple Contraints",
"description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or",
"link2":"http://www.avtainsys.com/articles/Triple_Contraints",
"size":"3.3kb"
},
{
"num":"-1"
}
],
"other_pages":[
{
"title":"1",
"link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=",
"active":"true"
}
]
}
Now, how would I do this programmatically? And (perhaps more importantly), is there a more elegant way of accomplishing this? And you should know that PHP is the only language I can run on my shared hosting account, so a Java solution for example would not work for me.
In search_result.html, you can surround the , at the end of the foreach loop with condition to only print if the index is strictly less than the number of pages - 1.