<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: thisisreen</title>
    <description>The latest articles on DEV Community by thisisreen (@reen).</description>
    <link>https://dev.to/reen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1074612%2F74c45064-d232-455d-8e4c-bf79c207a99a.png</url>
      <title>DEV Community: thisisreen</title>
      <link>https://dev.to/reen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reen"/>
    <language>en</language>
    <item>
      <title>Can I get a help connecting my project to mongodb</title>
      <dc:creator>thisisreen</dc:creator>
      <pubDate>Mon, 01 May 2023 02:41:27 +0000</pubDate>
      <link>https://dev.to/reen/can-i-get-a-help-connecting-my-project-to-mongodb-5c56</link>
      <guid>https://dev.to/reen/can-i-get-a-help-connecting-my-project-to-mongodb-5c56</guid>
      <description>&lt;h1&gt;
  
  
  I have a problem connecting my component to the database
&lt;/h1&gt;

&lt;p&gt;this is my registration.component.ts file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import { ApiService } from './api.service';

@Component({
  selector: 'app-registration',
  templateUrl: './registration.component.html',
  styleUrls: ['./registration.component.css']
})
export class RegistrationComponent {
  constructor(private apiService: ApiService) { }
  username!: string;
  email!:string;
  password!:string;

  submitRegistrationForm() {
    const registrationData = {
      username: this.username,
      email: this.email,
      password: this.password,
    };
    this.apiService.createUser(registrationData).subscribe(response =&amp;gt; {
      console.log('User created successfully!');
    }, error =&amp;gt; {
      console.log('Error creating user: ', error);
    });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;registration.component.html file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
  &amp;lt;meta charset="utf-8"&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1"&amp;gt;
  &amp;lt;title&amp;gt;Bootstrap demo&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;div class="container py-5 registration-container"&amp;gt;
    &amp;lt;h1 class="text-center mb-4"&amp;gt;MS Account Registration&amp;lt;/h1&amp;gt;
    &amp;lt;div class="row justify-content-center"&amp;gt;
      &amp;lt;div class="col-md-6"&amp;gt;
          &amp;lt;form (submit)="submitRegistrationForm()"&amp;gt;
            &amp;lt;div class="mb-3"&amp;gt;
              &amp;lt;label for="username" class="form-label"&amp;gt;Username&amp;lt;/label&amp;gt;
              &amp;lt;input type="text" id="username" name="username" class="form-control" required [(ngModel)]="username"&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="mb-3"&amp;gt;
              &amp;lt;label for="email" class="form-label"&amp;gt;Email&amp;lt;/label&amp;gt;
              &amp;lt;input type="email" id="email" name="email" class="form-control" required [(ngModel)]="email"&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="mb-3"&amp;gt;
              &amp;lt;label for="password" class="form-label"&amp;gt;Password&amp;lt;/label&amp;gt;
              &amp;lt;input type="password" id="password" name="password" class="form-control" required [(ngModel)]="password"&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="mb-3"&amp;gt;
              &amp;lt;label for="confirm-password" class="form-label"&amp;gt;Confirm Password&amp;lt;/label&amp;gt;
              &amp;lt;input type="password" id="confirm-password" name="confirm-password" class="form-control" required&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="mb-2 form-check"&amp;gt;
              &amp;lt;input type="checkbox" id="terms" name="terms" class="form-check-input" required&amp;gt;
              &amp;lt;label for="terms" class="form-check-label"&amp;gt;I agree to the terms and conditions&amp;lt;/label&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="text-center"&amp;gt;
              &amp;lt;button type="submit" class="btn btn-primary"&amp;gt;Register&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/form&amp;gt;

      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;api.service.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  private API_URL = 'http://localhost:3000';

  constructor(private http: HttpClient) { }

  createUser(userData: any) {
    return this.http.post(`${this.API_URL}/api/users`, userData);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;server.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const cors = require('cors');

const app = express();

// Configure middleware
app.use(express.json());
app.use(cors());

// Connect to the database
mongoose.connect('mongodb://localhost:27017/registerdb', { useNewUrlParser: true, useUnifiedTopology: true });


// Define the User schema
const UserSchema = new mongoose.Schema({
  username: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
});

const User = mongoose.model('User', UserSchema);

// Define the API routes
app.post('/api/users', async (req, res) =&amp;gt; {
  const { username, email, password } = req.body;
  const user = new User({ username, email, password });
  try {
    await user.save();
    res.status(201).json({ message: 'User created successfully!' });
  } catch (err) {
    console.error(err);
    res.status(500).json({ message: 'Internal server error' });
  }
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =&amp;gt; {
  console.log(`Server started on port ${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the error I'm encountering &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N8plp2iJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p80gj2t7vn177srk691o.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N8plp2iJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p80gj2t7vn177srk691o.PNG" alt="Image description" width="531" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
