Nodejs Express multi category route schema - node.js

I list the articles under the category. but there are more than one subcategory. I want to write these together in connection. I also want the page number in the form of /page/2. What kind of schema should I use here.
This is the current route. But isn't work
([a-z0-9-_/]+/)?:slug([0-9a-z-_]+)(/page/:page([0-9]+))?$
Link structure
category/[category]/[sub]/[sub-sub]/page/[number]
Sample Link
category/language/mobile/swift/page/2
And I want the last subcategory name. (swift)

Related

How to create one-to-many and many-to-many relations in Strapi v.4?

I want to create relations between Post and Tag as follows.
Post has many Tags (one-to-many)
Tags have and belongs to many Posts (many-to-many)
I created collection types for Post and Tag.
Set them accessible for public user.
I created 1 post with 3 tags.
Now, when I try to see them at http://localhost:1337/api/posts
I don’t see nested elements, i.e. tags…
Am I missing anything ?
And lastly, I was not able to create Many-to-Many relations between Post and Tag with the following error.
I understand that Tags field already exists, but I thought this is how supposed to be set.
I got reply from Strapi forum, so decided to share here.
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html
According to the docs above, to pull (in my case) 1st level nested elements I have to add populate[0] parameter like so:
http://localhost:1337/api/posts?populate[0]=tags

How I can build a custom/dynamic uploading form in Django 2.1 so that the user can upload the data within the required elements

I am building an app in Django 2.1 which allows users to upload posts. I have achieved this using Django forms and sent the data back to DB (this is simple as it has post title and the post itself). Now I want to enhance this with the dynamic generation of elements while user uploads the data i.e. how can I implement the following requirement :
How can we allow users to add unknown number of images while uploading a post OR for a technical post user wants to add code snippets (dynamic number of snippets anywhere in the post). The post should be saved in the DB and then will be rendered.
How would the DB schema be defined in this scenario, because the number of elements are undefined initially.
Take a simple example to understand the requirement: Start writing a post on blogger and there would be n number of UI elements available to add in the post at the runtime.
I have got a reference while googling:
https://www.caktusgroup.com/blog/2018/05/07/creating-dynamic-forms-django/
I'm a newbie and want to know how I can technically achieve this. I would really appreciate any help.
Thanks
How would the DB schema be defined in this scenario, because the number of elements are undefined initially.
This is the purpose of relations in a Relational database.
When an object Core contains/is related to multiple objects Extension, we call that relation a One To Many relationship (as One Core possesses/has Many Extensions).
In SQL, and also in Django, those OneToMany are represented by Foreign Keys.
So in your case, you'll do it with something like this:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=xxx)
content = models.TextField()
class Image(models.Model):
file = models.ImageField(...)
post = models.ForeignKey('Post', on_delete=models.CASCADE)
class Snippet(models.Model):
content = models.TextField()
post = models.ForeignKey('Post', on_delete=models.CASCADE)
How can we allow users to add unknown number of images while uploading a post OR for a technical post user wants to add code snippets (dynamic number of snippets anywhere in the post).
For your frontend, you can use the Django Formsets which can be used in your case (as you have multiple input corresponding to the same Model).
With some little javascript, you can use the Django empty_form to dynamically display a new form when your end user fills the one already displayed, for example.

Live/Dynamic query for MongoDB NodeJS

I'm making a NodeJS Express MongoDB app with the database of various books.
I have a list of books with information like Book Name, Author name, ISBN and so on.
View of the list here
I want to create a query function such that the page finds for relevant fields as I type along in the search text field.
Any suggestions how to implement it?
If you want to make text queries you can use the $text index on mongodb, and index the fields you need. this will allow you to search the text on all indexed fields.
you can read more about that here: https://docs.mongodb.com/manual/reference/operator/query/text/

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the association when doing a listing of all items for performance reasons. But when viewing a single item, it would be nice to complete the population.
For example, say one ticket can have many comments. I don't care about the comments when fetching a case listing but would be important when viewing a specific case. I took a guess at how it could function but it fails:
localhost:1337/tickets?populate=false
Update
I implemented the above functionality within balderdashy/sails#1695. The only change is that you selectively choose which associations to populate using:
localhost:1337/tickets?populate=[] // Don't populate anything
localhost:1337/tickets?populate=[comments] // Only populate comments
This would override whatever is defined for populate within your blueprint config.
You just need to separate your assosiactions via comma, just like this:
localhost:1337/tickets?populate=comments,owner&SOME_OTHER_PARAMS

Node.js + Mongoose: Find data and get other data for each

I have two models Users and News. On the page which is written with Express framework are published news and under the news are comments. Inside News model is subdocument with comments which contains two fields - user (subfields:) { name, objectid } and comment. Because in addition to comment there is user's name, I would like to add some additional informations about it (like number of comments, link to website, ...).
And this is my question: How to get data of user (from Users model) for each comment from subdocument (from News model)?
Add a populate call to your find query to pull in the user details. I'm not quite clear on your schema, but something like:
News.find().populate('comments.userId').exec(...);
This relies on your schema defining userId as an ObjectId ref to Users.

Resources