Guide on utilizing two separate collections to store different types of data for an application's users

I am looking to create a database collection similar to

{username : "jack", password : "pass"}
for storing doctors' login information. I believe I can achieve this during signup by implementing the following code:

var Doctor = mongoose.model("doctor", authSchema);
module.exports = Doctor;

The code snippet above would generate a collection named doctor with the provided data from the form, as shown below.

app.post("/signup", function(req, res){

     Doctor.create({username: req.body.username , password : req.body.password}) 
    res.redirect("/login");
})

It might be beneficial to have separate collections for doctors and patients. This way, doctors can manage their profile information and add patients to their database. A key question is how to link these two collections. Considering that doctors will need to sign in using passport local and access only their own patient records, one approach could involve creating a "view patients" link that utilizes passport's req.userto fetch all patients associated with that specific doctor. Is this the correct method to establish this relationship? As I navigate through this learning process independently, the concept of utilizing two collections is new to me, and I want to ensure I am heading in the right direction.

Answer №1

To utilize your model, follow these steps:

 let employee = new Employee();
 employee.username = req.body.username;
 password = req.body.password;
 employee.save();

Alternatively, you can use:

let employee = new Employee();
employee.username = req.body.username;
password = req.body.password;
doctor.save(function (err, doctor) {
      if (err) ..
 })

It's important not to store passwords as plain text; consider using bcrypt to create a hash for security. Check out an example of how to secure passwords here: https://github.com/asimzsaeed/mongodb_node_multi_tenancy_startup/blob/master/server/lib/services/user/model.js

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is server-side rendering necessary for `browserHistory` in React Router?

I am a beginner in the world of React and currently diving into the complexities of routing. While hashHistory (/#/paths/like/this) proves to be functional, browserHistory (/paths/like/this) appears much cleaner. However, I noticed that when reopening URLs ...

transferring a string parameter from PHP to a JavaScript function

I have been searching for a way to transfer a string (stored as a variable $x) from PHP to JavaScript. I came across several code solutions, but I am wondering if these strings need to be declared as global variables? Even after declaring it as a global va ...

The current version of 'buffer' is outdated. To resolve this issue, please upgrade to v4.9.2 or higher

Having some trouble integrating aws-amplify and aws-amplify-react modules into my application. After running: npm install --save aws-amplify aws-amplify-react I encountered multiple warning messages related to missing or deprecated dependencies. Althoug ...

React - Implementing toggling of a field within a Component Slot

I find myself in a peculiar situation. I am working on a component that contains a slot. Within this slot, there needs to be an input field for a name. Initially, the input field should be disabled until a web request is made within the component. Upon com ...

Export data from Angular Material data table to Excel format

I'm currently utilizing the angular material data table to showcase data in a tabular layout. I have a requirement to add a feature that enables the export of tabular data to an Excel sheet. Unfortunately, I haven't been able to locate any resour ...

How can you prevent the 'Script not responding' error when using Reverse AJAX / Comet?

My worker thread is responsible for sending requests to the server using XMLHttpRequest. The request is directed to a php file which checks the integrity of client information. If the client requires new data, it is sent. Otherwise, the server continuously ...

A step-by-step guide on utilizing the reduce function to calculate the overall count of a user's GitHub repositories

I need help finding the total number of repositories for a specific user. Take a look at my code below: Javascript const url = "https://api.github.com/users/oyerohabib/repos?per_page=50"; const fetchRepos = async () => { response = await f ...

Why isn't CSS showing up in preview mode on the network tab?

I have been using nextjs for server-side rendering and I am encountering an issue where my CSS class is not being applied on the preview tab, it only works on the client side. Any idea why this is happening? Here is the code snippet: https://codesandbox.io ...

Implementing JavaScript for showcasing weights

I've encountered a JavaScript function that modifies user preferences for weight units, allowing them to choose between metric and imperial measurements. When displaying weights on my page, I typically present them as follows: This is a brief explan ...

What is the best way to add data-content to hr:after using JavaScript?

Adding style to an element in javascript can be simple. For example: myElement.style.color = "red"; However, what if I wanted to achieve something like this: hr:after { content: "myRuntimeValue" } Is there a way to do this using javascript? ...

Issue: It seems like there is an error with using functions as a React child. Uncertain about the exact location

This specific issue is one of the two errors I have come across in the same application referenced in my previous inquiry. The first error encountered is as follows: Warning: Functions are not valid as a React child. This may occur if you mistakenly return ...

Integrate an item into the request body utilizing the $.load() function in jQuery

Currently, I am using a POST request via jQuery's $.load function to retrieve data to display in a window. This is what I am currently doing: var testObject = { thing1: 'data1', thing2: 'data2', thing3: &a ...

What is the best method for exporting a MapboxGL map?

I am currently utilizing mapboxGL to display maps on a website, and I am interested in exporting the map as an image with the GeoJSON data that has been plotted. I attempted to use the leaflet plugin for this purpose, but it was unable to render clusters ...

finding it difficult to display my components when incorporating ROUTE in my ReactJS project

Can anyone help me with the code for app.js? I've been trying to implement ROUTE but my components are not rendering correctly. import './App.css'; import Home from './pages/Home'; import Rooms from './pages/Rooms'; impor ...

Can you explain the distinction between Vue's 'v-on' directive and vue.$on method?

If I have two sibling components set up like this: <div id="root2"> <some-component>First</some-component> <some-component>Second</some-component> </div> ... and these components are coded as follows: Vue.comp ...

Encountering an issue with obtaining req.param that is undefined within

The router is currently displaying undefined for req.params.id. I have set up a route for comments as shown in the code snippet below from my app.js file. While I can successfully access the /campgrounds/:id/comments routes, the req.params.id remains undef ...

Utilizing AJAX to integrate the Google Maps Direction API

Currently, I am developing a small website that utilizes the Google Maps API and AJAX to load directions based on selected locations from a dropdown menu. The database stores latitude and longitude coordinates of various locations, which are retrieved via ...

What purpose does the array.pop()!(object) syntax serve within Codemirror?

Within the Codemirror 6 documentation and at line 41 of the code, there is a snippet that reads: while (pending.length) pending.pop()!(data.updates) I'm curious about the meaning of this syntax. It appears to be TypeScript specific. How would this lo ...

AngularJS - the element of surprise in execution sequence

Encountering a puzzling issue that exclusively affects Internet Explorer (any version) and not Chrome. An "items" array is stored within the "doc" object. Users have the capability to edit items, which essentially deletes the item but retains its content ...

Is it necessary to enable validation for an Angular reactive form toggle?

Can you help with this issue I'm having? I have a radio button that asks the user if they have a massage certificate. If they answer yes, a file input should be displayed. By default, the file input should not be required, but if the user selects yes, ...