about user authentication and storing user data [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 17 hours ago.
Improve this question
I always had this doubt when I think about user data. If I created a site, for example a marketplace, when I'm going to store the data in the backend should I simply create a big array like this?
const user = [
userName: "john",
userWallet: 122,
userProfilePic: "https://blbabablabl.com/ewxase"
userCart: [
{
productName: "Air Jordan 1",
price: 280,
productImage: https://www.blablabla.com/image
}
{
productName: "Louis Vuitton Bag",
price: 900,
productImage: https://www.blablabla.com/image
}
...
]
]
and then for every user I create an array?
is this right?
what companies do in this situation?
where can I learn more about storing things in the backend?
I'm really lost when it comes to backend.
(I'm using firebase in my project cuz I don't have the interest to study back-end. For now, I'm focusing more on the front-end)

For smaller use cases, you can definitely use arrays. However, this isn't scalable - you'll find that this approach can become very inefficient as your user base grows. Most companies store their user data in a database, such as MongoDB or MySQL. Databases allow you to create a "users" collection with columns for each of the attributes such as username, wallet, profile pic, etc. For more additional info about databases: https://www.w3schools.in/sql/database-concepts

The best way to do that would be to create separate sub-collection name carts under document of given user and store all necessary attributes for each cart item as document of that subcollection.
eg. You have user documents under users collection, then
cart document path would be as /users/3y1x1Dy6FYOGnSW0Sa4f1EUhiIo1/carts/WADVd2AZ91d7m9kSq3GP
See screenshots below:
Alternatively, you can create same level collection for carts and store userId field on cart's document.

Related

How to perform delete operation? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
For a production level web app, How anyone should perform a delete operation. Like if we have stored an array of products in global store(eg. Redux) on the client side. Now I perform a delete request to the backend to delete a product with id "001". Now should I get an updated list of products from the backend to display or I should just filter out the globally available state from the store?
I want to know the optimal way.
Do Post Request with id to an api to delete. Get that id in backend delete it and then return updated records as result
If you are building something like an e-commerce website you probably shouldn't store a copy of data about all your products in Redux.
If you store data about products on server you can use library like React Query to fetch it to display on client or mutate data stored on server(delete for example). React Query also has cashing for optimisation so you don't fetch data on every render. You can think of it as synchronization of your server and client state.
Redux is used to store client state like products in cart. You can use redux when you create data on client and manipulate it while it is stored in redux. Then you can send it to server when needed.
There is a great video about server state and client state with explanation and coding examples: https://www.youtube.com/watch?v=5-1LM2NySR0&t=891s&ab_channel=Theo-ping%E2%80%A4gg

Can we retrive data of two tables in nodejs with mongoose [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working one project there is some relational concept so i want to retrieve data from two different tables in same query , can i do this ? i am following below link but i am not getting exact that i wants.
Mongoose/mongoDB query joins.. but I come from a sql background
I have also review this links
https://start.jcolemorrison.com/mongodb-joins-with-mongoosejs/
I have review this mongoose links, here i am getting something positive.
http://mongoosejs.com/docs/populate.html
What i have done so far is like below:
Create Models for project and project_category and try to get all project with category details. I have update my code at below link to review :
https://www.protectedtext.com/rushabhcode
mongooses have populate in built function to join two table
in schema just add ref for linking table
suppose you want to join two table
table 1 :User
table 2 :posts
in posts schema add
posted_by: {type: Schema.Types.ObjectId, ref: 'User', required: true},
and the time of query just add populate
suppose
postModel.find({}).populate({path:'posted_by'}).exec(function(err,response){
})
this will help you get all post value with user details who has posted

Creating a room with Firebase for a web app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Im currently building a web app based in firebase and want users the ability to create and join game rooms. Currently i am having an issue trying to figure out how to create the room where others can join. I was wondering if anyone can help me get started on this issue. Just an FYI each user who will be creating and joining the room will have their own account with unique ID if that matters
Firebase doesn't have any concept of rooms. There are parent and child nodes which are code wise handled through key: value pairs, very similar to a Dictionary.
There are also no users in Firebase either, other than the user id's (uid) and associated data that are created when new user is created in Firebase and stored internally on the server. That data is used for authentication. If other data needs to be stored about a user, a name perhaps, it's done within the database itself.
Keeping it simple, let's say we want to store information about users and rooms.
users
uid_0
name: "Bill"
uid_1
name: "Ted"
uid_27
name: "The Doctor"
The uid's are the ones that are created when a new user creates an account in Firebase. Your app will collect the uid that Firebase provides and write it and any other user data to the users node; in this case the users name.
Then the rooms
rooms
room_0
room_name: "Phone Booth"
description: "Time travelling in style; Excellent!"
uid_0: true
uid_1: true
room_1
room_name: "Tardis"
description: "It's bigger on the inside"
uid_27: true
In this case room_0, the Phone Booth, has both Bill and Ted in it and room_1 has The Doctor in it.
This is not the only way to structure the data and your structure may vary from this a lot, but it's a place to start.
Note: key names should almost always be created with push(). I used room_0 etc as a placeholder. Best practice is to disassociate main parent key names from the data it contains. i.e. don't hard code key names with something like an email address - that's bad news.

How Do I store and display (automatically) stories that users can submit on my website [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm Making a website for a friend where people will submit stories. These will be short stories, presented on the website with a Title, a "by" section, ideally a picture of the author, and finally the story.
Now I've already got the design of the site up, but this is partly because I know how to use HTML and CSS. Though I know a bit of Jquery, Javascript, PHP, and SQL, I'm at a loss for how I should be saving any of the files and displaying them.
Assuming I want the website to automatically take in any stories, what is the best way to go about doing this? Right now, we have a contact form that would connect to our email where the stories can be sent it... is there a better way to do this so we can somehow just approve the story, then save the story somewhere, then have the website grab the story from where all stories are saved, and finally display it on the website?
If there's any hints or things I can look into that would help me accomplish all of this, I would greatly appreciate it!
Thanks in advance!
There is no quick and simple answer for this.
You need to store user-submitted data somewhere - preferrably a database.
You could try to start with an SQL database like SQLite or MySQL.
These databases hold tables that can stores values - values your users submitted
Example: Stories Table
story_id | author | img | story_text |
-----------+--------------+-------------+--------------------+
1 | john | john.jpg | I bought candy. |
2 | mary | mary.jpg | my pony died! :( |
author, img, and story_text would be values sent to the server from an input form on the website.
The server's language (ie, php) would take these values and store them in the database, something like this
$st = $db->prepare("INSERT INTO stories_table (author, img, story_text ) values ( 'john', 'john.jpg', 'I bought candy'. )");
$st->execute();
Hopefully you can see how the above line would store things in the table i drew above.
Unfortunately It's nothing something that can be answered here. It's something that requires a whole book. (don't forget you also need to use PHP to grab form values that the user inputted), but hopefully this gives you an idea of one of the ways it can be done
For s*** and giggles, here's an inefficient way of doing it without a database - using a text file instead.
* Step 1 *
Learn how to grab form values with PHP. Endless tutorials on google.
* Step 2 *
In your server, create a file called stories.json.
Then, when a user submits a form, you can use PHP to append values to to stories.json. Let's say i submit a story. stories.json would look like this
{
"author": "sqram",
"image": "sqram.jpg",
"story": "Today, I worked."
}
Now you have used PHP to store the information in a file, and can retrieve that information however you want. You could ajax it with javascript and show my story. that stories.json file is essentially being a database for you.
I don't recommend doing it this way. In fact, not a single person will. But it's an actual way of doing.
What you're looking for is to use PHP and MySQL with a database. What you want to do is why PHP and MySQL are a thing.
Have a database where the stories are stored, and there will be a column with if the story is approved or not. If you approve the story then it will display.
(This is just a rough overview of what you'll need to code.)

Express js and Mongo DB database definition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I started learning node these days and Im using Express js framework.
I found this great tutorial from Christophe Coenraets with all stuff I need since I have a little bit experience in Backbone js.
The thing that's confusing me the most in Node js is working with databases . (I'm only familiar with MYSQL since i was using PHP)
For example in this tutorial , in this file
https://github.com/ccoenraets/nodecellar/blob/master/routes/wines.js
He use "populateDB" function to populate the database for the first time whit some doomy data when app is started,
Now since I was using PHP , I what to do the same thing I was doing there , I want to create database structure or something like shell so I can have an image in my head to to represent the data , for example I saw some example is "mongoose" module "mongo db object modeling" that there are some things like "Schema" or something like that .
Can somebody explain me right logic on working with databases in Node js, how you start when you are creating your app, the process and stuff? its little bit confusing to start working and to dont know from the start how the database will look like.
Tnx a lot ! :)
The thing about NoSQL databases like mongodb is that you no longer have the traditional columns, rows, and tables. We now have fields, documents and collections. See SQL to MongoDB Mapping Chart
Mongoose is a framework that provides a schema-based solution to modeling your application data. It's not necessary to use but does provide you with the option of having predefined schemas and models similar to that of SQL schemas, instead of inserting straight up JSON into the collections. See Why does mongoose use schema when mongodb's benefit is supposed to be that it's schema-less? for more details.
For example:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Schema = mongoose.Schema;
var catSchema = new Schema({ //cat schema
name: String,
age: Number
});
var Cat = mongoose.model('Cat', catSchema); //cat model
var kitty = new Cat({ name: 'Zildjian', age: 5 }); //kitty is a cat
kitty.save(function (err) {
if (err) // ...
console.log('Error saving to the database');
});
...and now you have a kitty saved in your mongodb
So to start: download and install mongod and mongodb, npm install mongoose, create your schema and model via mongoose.
To query/find/retrieve documents/rows from the database, use the model.find(), ex:
// named Zildjian and at least 5
Cat.find({ name: 'Zildjian', age: { $gte: 5 }});
For more details, please see model.find API

Categories