最近有:
Angular:5.2
Firebase SDK:4.13.0
npm:6.0.0
node:v8.11.1
尝试使用以下代码创建使用firebase的用户注册:
signup.component.html:
<form (ngSubmit)="onSignup(form)" #form="ngForm">
<div class="form-group">
<label for="email">Email</label>
<input name="email" id="email" type="email" class="form-control" ngModel>
</div>
<div class="form-group">
<label for="Password">Password</label>
<input name="Password" id="Password" type="text" class="form-control" ngModel>
</div>
<button type="submit" class="btn btn-success">Sign Up</button>
</form>
signup.component.ts:
import { Component, OnInit } from '@angular/core';
import {NgForm} from '@angular/forms';
import {AuthService} from '../auth.service';
@Component({
selector: 'app-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit {
constructor(private authService: AuthService) { }
ngOnInit() {
}
onSignup(form: NgForm) {
const email = form.value.email;
const password = form.value.password;
this.authService.signupUser(email, password);
}
}
并在AuthService.service.ts中:
import * as firebase from 'firebase';
import {Injectable} from '@angular/core';
import {Router} from '@angular/router';
@Injectable()
export class AuthService{
token: string;
constructor(private router: Router) {}
signupUser(email: string, password: string) {
firebase.auth().createUserWithEmailAndPassword(email, password)
.catch(
error => console.log(error)
);
}}
但是一旦我点击“注册”按钮,它仍会在控制台上抛出此错误:
ERROR
N {code: "auth/argument-error", message: "createUserWithEmailAndPassword failed: Second argument "password" must be a valid string.", ngDebugContext: DebugContext_, ngErrorLogger: ƒ}
code:
"auth/argument-error"
message:
"createUserWithEmailAndPassword failed: Second argument "password" must be a valid string."
那么有可能使用firebase SDK使用此方法创建新用户吗?!
答案 0 :(得分:0)
SDK是正确的,看起来您的HTML中有拼写错误。
<input name="Password" id="Password" type="text" class="form-control" ngModel>
id
和name
都是大写属性Password
- 但在您的signupUser
函数中,您尝试访问小写属性password
尝试将HTML更新为<input name="password" id="password" type="text" class="form-control" ngModel>