Отправка данных из компонента в ангуляру на Localhost . создать locallhost на стороне ангуляра (angular 15)?
в компоненте ангуляра естб форма с инпутами ее нужна отправлять на mongodb данные я сохраняю в юзерз но не могу передать на сервер для пренаправки их с сервера на mongo
серверный index.js
import mongoose from 'mongoose';
import cors from 'cors';
const url = 'mongodb+srv://annaozinkovska:[email protected]/hydra';
mongoose.connect(url)
.then(() => {
console.log('Connected to DB');
})
.catch((err) => {
console.log(`DB connection error: ${err}`);
});
const PORT = 4235;
const app = express();
const Schema = mongoose.Schema;
const UsersSchema
= new Schema({
name: String,
lastname: String,
email: String,
number: Number,
text: String,
title: String,
});
const Users = mongoose.model('users', UsersSchema);
app.use(cors());
app.get('/', (req, res) => {
Users.find()
.then(users => {
const userHtml = users.map(users => users);
res.send(userHtml);
})
.catch(error => {
console.error(error);
});
});
app.post('/users', (req, res) => {
const { name, lastname, email, number, text, title } = req.body;
const newUser = new Users({
name,
lastname,
email,
number,
text,
title
});
newUser.save()
.then(user => {
res.status(201).json(user);
})
.catch(error => {
console.error(error);
res.status(500).json({ error: 'Failed to create user' });
});
});
app.put('/users/:id', (req, res) => {
const { name, lastname, email, number, text, title } = req.body;
const userId = req.params.id;
Users.findByIdAndUpdate(userId, {
name,
lastname,
email,
number,
text,
title
}, { new: true })
.then(updatedUser => {
res.json(updatedUser);
})
.catch(error => {
console.error(error);
res.status(500).json({ error: 'Failed to update user' });
});
});
app.delete('/users/:id', (req, res) => {
const userId = req.params.id;
Users.findByIdAndRemove(userId)
.then(deletedUser => {
res.json(deletedUser);
})
.catch(error => {
console.error(error);
res.status(500).json({ error: 'Failed to delete user' });
});
});
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});
компонент который принимает инпути
import { Component, Input } from '@angular/core';
import { NgForm } from '@angular/forms';
import { SlideUsersInterface } from '../PostSlider/types/slideusers.interface';
@Component({
selector: 'app-join',
templateUrl: './join.component.html',
styleUrls: ['./join.component.scss']
})
export class JoinComponent {
@Input() user={
name:String,
lastname:String,
email: String,
number:String,
Subject:String,
text:String,
}
onSubmit( data:NgForm){
this.user.name=data.value.name
this.user.lastname=data.value.lastname
this.user.email=data.value.email
this.user.Subject=data.value.Subject
this.user.number=data.value.number
this.user.text=data.value.text
console.log(this.user);
}
компонент который виводит инпути в администраторе
import { HttpClient } from '@angular/common/http';
import { Component, Input } from '@angular/core';
import { SlideUsersInterface } from '../PostSlider/types/slideusers.interface';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent {
@Input() slides: SlideUsersInterface[]=[]
constructor( private http:HttpClient ){
this.http.get('http://localhost:4234')
.subscribe((data)=> this.addusers(data));
}
addusers(data: any){
this.slides=data;
console.log(this.slides);
}
// inserttodb(){
// this.user
// }
} ```