Angular Submit不会在数据库中插入所有字段
在以下对MongoDB数据库的插入中,一些字段没有插入到数据库中,它只是插入:email和password,其他字段没有,并且在编译时不会在浏览器控制台或命令控制台中引发任何错误 register.component.tsAngular Submit不会在数据库中插入所有字段,angular,mongodb,Angular,Mongodb,在以下对MongoDB数据库的插入中,一些字段没有插入到数据库中,它只是插入:email和password,其他字段没有,并且在编译时不会在浏览器控制台或命令控制台中引发任何错误 register.component.ts import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Va
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { NB_AUTH_OPTIONS, NbAuthSocialLink, NbAuthService, NbAuthResult } from '@nebular/auth';
import { getDeepFromObject } from '../../helpers';
import { EMAIL_PATTERN, NUMBERS_PATTERN } from '../constants';
@Component({
selector: 'ngx-register',
styleUrls: ['./register.component.scss'],
templateUrl: './register.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NgxRegisterComponent implements OnInit {
minLength: number = this.getConfigValue('forms.validation.password.minLength');
maxLength: number = this.getConfigValue('forms.validation.password.maxLength');
isFirstNameRequired: boolean = this.getConfigValue('forms.validation.firstName.required');
islastNameRequired: boolean = this.getConfigValue('forms.validation.lastName.required');
isEmailRequired: boolean = this.getConfigValue('forms.validation.email.required');
isPasswordRequired: boolean = this.getConfigValue('forms.validation.password.required');
redirectDelay: number = this.getConfigValue('forms.register.redirectDelay');
showMessages: any = this.getConfigValue('forms.register.showMessages');
strategy: string = this.getConfigValue('forms.register.strategy');
socialLinks: NbAuthSocialLink[] = this.getConfigValue('forms.login.socialLinks');
submitted = false;
errors: string[] = [];
messages: string[] = [];
user: any = {};
registerForm: FormGroup;
constructor(protected service: NbAuthService,
@Inject(NB_AUTH_OPTIONS) protected options = {},
protected cd: ChangeDetectorRef,
private fb: FormBuilder,
protected router: Router) { }
// get fullName() { return this.registerForm.get('fullName'); }
// get email() { return this.registerForm.get('email'); }
get password() { return this.registerForm.get('password'); }
get confirmPassword() { return this.registerForm.get('confirmPassword'); }
// get terms() { return this.registerForm.get('terms'); }
get firstName() { return this.registerForm.get('firstName'); }
get lastName() { return this.registerForm.get('lastName'); }
get login() { return this.registerForm.get('login'); }
get email() { return this.registerForm.get('email'); }
get age() { return this.registerForm.get('age'); }
get street() { return this.registerForm.get('address').get('street'); }
get city() { return this.registerForm.get('address').get('city'); }
get zipCode() { return this.registerForm.get('address').get('zipCode'); }
ngOnInit(): void {
const firstNameValidators = [
];
this.isFirstNameRequired && firstNameValidators.push(Validators.required);
const lastNameValidators = [
];
this.islastNameRequired && lastNameValidators.push(Validators.required);
const emailValidators = [
Validators.pattern(EMAIL_PATTERN),
];
this.isEmailRequired && emailValidators.push(Validators.required);
const passwordValidators = [
Validators.minLength(this.minLength),
Validators.maxLength(this.maxLength),
];
this.isPasswordRequired && passwordValidators.push(Validators.required);
this.registerForm = this.fb.group({
fullName: this.fb.control('', [...firstNameValidators]) + " " + this.fb.control('', [...firstNameValidators]),
firstName: this.fb.control('', [...firstNameValidators]),
lastName: this.fb.control('', [...lastNameValidators]),
email: this.fb.control('', [...emailValidators]),
login: this.fb.control('', [Validators.required, Validators.minLength(6), Validators.maxLength(20)]),
age: this.fb.control('', [Validators.required, Validators.min(1),
Validators.max(120), Validators.pattern(NUMBERS_PATTERN)]),
password: this.fb.control('', [...passwordValidators]),
confirmPassword: this.fb.control('', [...passwordValidators]),
address: this.fb.group({
street: this.fb.control(''),
city: this.fb.control(''),
zipCode: this.fb.control(''),
}),
});
}
register(): void {
this.user = this.registerForm.value;
this.errors = this.messages = [];
this.submitted = true;
this.service.register(this.strategy, this.user).subscribe((result: NbAuthResult) => {
this.submitted = false;
if (result.isSuccess()) {
this.messages = result.getMessages();
} else {
this.errors = result.getErrors();
}
const redirect = result.getRedirect();
if (redirect) {
setTimeout(() => {
return this.router.navigateByUrl(redirect);
}, this.redirectDelay);
}
this.cd.detectChanges();
});
}
getConfigValue(key: string): any {
return getDeepFromObject(this.options, key, null);
}
}
register.component.html
<div [formGroup]="registerForm">
<div class="form-control-group">
<label for="input-name">Fisrt Name:</label>
<input nbInput
autofocus
fullWidth
fieldSize="large"
id="input-name"
formControlName="firstName"
[status]="firstName.dirty ? (firstName.invalid ? 'danger' : 'success') : ''"
[attr.aria-invalid]="firstName.invalid && firstName.touched ? true : null"
placeholder="Full name">
<ngx-validation-message label="Full name" [showRequired]="firstName?.errors?.required && firstName.touched">
</ngx-validation-message>
</div>
<div class="form-group-group">
<label for="lastName">Last Name</label>
<input nbInput id="lastName" formControlName="lastName" fieldSize="large"
[status]="(lastName?.hasError('minLength') || lastName?.hasError('maxLength')) ? 'danger' : 'primary'"
class="form-control" placeholder="Last Name">
<ngx-validation-message label="Last Name" [showMinLength]="lastName?.hasError('minLength')"
[showMaxLength]="lastName?.hasError('maxLength')" minLength=3 maxLength=20></ngx-validation-message>
</div>
<div class="form-group-group">
<label for="inputLogin">Login</label>
<input nbInput id="inputLogin" formControlName="login" fieldSize="large"
[status]="(login?.errors?.required || login?.hasError('minLength') || login?.hasError('maxLength')) ? 'danger' : 'primary'"
class="form-control" placeholder="Login">
<ngx-validation-message label="Login" [showMinLength]="login?.hasError('minLength')"
[showMaxLength]="login?.hasError('maxLength')" [showRequired]="login?.errors?.required" minLength=6
maxLength=20></ngx-validation-message>
</div>
<div class="form-group-group">
<label for="inputAge">Age</label>
<input nbInput id="inputAge" formControlName="age" fieldSize="large"
[status]="(age?.errors?.min || age?.errors?.max || age?.hasError('pattern')) ? 'danger' : 'primary'"
class="form-control" placeholder="Age">
<ngx-validation-message label="Age" [showMin]="age?.errors?.min" [showMax]="age?.errors?.max"
[showRequired]="age?.errors?.required"
[showPattern]="age?.hasError('pattern')" min=1 max=120></ngx-validation-message>
</div>
<div class="form-group-group">
<label for="inputEmail">Email</label>
<input nbInput id="inputEmail" formControlName="email" fieldSize="large"
[status]="(email?.errors?.required || email?.hasError('pattern')) ? 'danger' : 'primary'" class="form-control"
placeholder="Email">
<ngx-validation-message label="Email" [showPattern]="email?.hasError('pattern')"
[showRequired]="email?.errors?.required" min=1 max=120></ngx-validation-message>
</div>
<div class="form-control-group">
<label for="input-password">Password:</label>
<input nbInput
fullWidth
fieldSize="large"
id="input-password"
formControlName="password"
type="password"
[status]="password.dirty ? (password.invalid ? 'danger' : 'success') : ''"
placeholder="Password">
<ngx-validation-message label="Password" [showMinLength]="password?.hasError('minlength') && password.touched"
[showMaxLength]="password?.hasError('maxlength') && password.touched"
[showRequired]="password?.errors?.required && password.touched"
[minLength]="minLength"
[maxLength]="maxLength"></ngx-validation-message>
</div>
<div class="form-control-group">
<label for="input-re-password">Confirm Password:</label>
<input nbInput
fullWidth
fieldSize="large"
id="input-re-password"
formControlName="confirmPassword"
type="password"
[status]="confirmPassword.dirty ? (confirmPassword.invalid || password.value != confirmPassword.value ? 'danger' : 'success') : ''"
placeholder="Password">
<ngx-validation-message label="Confirm Password"
[showMinLength]="confirmPassword?.hasError('minlength') && confirmPassword.touched"
[showMaxLength]="confirmPassword?.hasError('maxlength') && confirmPassword.touched"
[showRequired]="confirmPassword?.errors?.required && confirmPassword.touched"
[minLength]="minLength"
[maxLength]="maxLength"></ngx-validation-message>
<p class="error-message" *ngIf="password.value != confirmPassword.value">
Password and confirm password does not match!
</p>
</div>
<div class="form-group-group" formGroupName="address">
<label for="inputStreet">Street</label>
<input fieldSize="large" nbInput class="form-control" id="inputStreet" placeholder="Street" formControlName="street">
</div>
<div class="form-group-group" formGroupName="address">
<label for="inputCity">City</label>
<input fieldSize="large" nbInput class="form-control" id="inputCity" placeholder="City" formControlName="city">
</div>
<div class="form-group-group" formGroupName="address">
<label for="inputZipCode">Zip Code</label>
<input fieldSize="large" nbInput class="form-control" id="inputZipCode" placeholder="Zip Code" formControlName="zipCode">
</div>
<!-- <div class="form-group-group">
<label for="inputZipCode">Role</label>
<nb-select fullWidth placeholder="Role" id="role" formControlName="role">
<nb-option value="user">User</nb-option>
<nb-option value="admin">Admin</nb-option>
</nb-select>
</div> -->
</div>
第一个名称:
姓
登录
年龄
电子邮件
密码:
确认密码:
密码和确认密码不匹配!
街头
城市
邮政编码
console.log(this.registerForm.value);
console.log(this.registerForm.value)并将其添加到您的问题中。控制台向我抛出所有值,我在那里将结果添加到问题中。您能显示您的this.service.register()代码吗?正在向mongo发送什么?`router.post('/sign-up',(req,res)=>{authService.register(req.body).然后(()=>res.send({message:'ok'})).catch(err=>res.status(400).send({error:err.message}));})`谢谢,你能展示一下你在邮件中发送的内容吗?它是否与您提交的表单正文图像匹配?