Javascript NodeJS尝试在mongo DB中查找数据时出错:未处理PromisejectionWarning
我正在做一个应用程序,在平均堆栈中登录。此登录的一个功能是社交登录,重点是当新用户想要使用facebook进入应用程序时,我必须检查此facebook是否已记录在我的mongoDB中,我有以下代码: 从前端页面: 1.-登录页面Javascript NodeJS尝试在mongo DB中查找数据时出错:未处理PromisejectionWarning,javascript,node.js,angular,express,authentication,Javascript,Node.js,Angular,Express,Authentication,我正在做一个应用程序,在平均堆栈中登录。此登录的一个功能是社交登录,重点是当新用户想要使用facebook进入应用程序时,我必须检查此facebook是否已记录在我的mongoDB中,我有以下代码: 从前端页面: 1.-登录页面 import { Component, OnInit } from '@angular/core'; import { AuthService } from "../services/auth.service"; import { Router }
import { Component, OnInit } from '@angular/core';
import { AuthService } from "../services/auth.service";
import { Router } from "@angular/router"
import { UserService } from "../services/user.service";
// Login con Google & Facebook
import { SocialAuthService } from "angularx-social-login";
import { FacebookLoginProvider, GoogleLoginProvider } from "angularx-social-login";
import { SocialUser } from "angularx-social-login";
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
socialuser : SocialUser;
loggedIn: boolean;
user = {
name: "",
email: "",
password: ""
}
options = []
constructor(
private authService: AuthService,
private router: Router,
private userService: UserService,
private socialauthService: SocialAuthService
) { }
ngOnInit() {
this.socialauthService.authState.subscribe((socialuser) => {
this.socialuser = socialuser;
this.user = {
name : this.socialuser.name,
email : this.socialuser.email,
password: "DefaultExample"
}
this.authService.log(this.user)
.subscribe(res => {
localStorage.setItem("token", res.token)
this.router.navigateByUrl("home");
})
this.loggedIn = (socialuser != null);
});
}
signInWithGoogle() {
this.socialauthService.signIn(GoogleLoginProvider.PROVIDER_ID);
}
signInWithFB() {
this.socialauthService.signIn(FacebookLoginProvider.PROVIDER_ID);
}
}
2.-认证服务:
import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import { Router } from "@angular/router";
@Injectable({
providedIn: 'root'
})
export class AuthService {
private URL = "http://localhost:3000/api"
constructor(
private http: HttpClient,
private router: Router
) { }
signUp(user) {
return this.http.post<any>(this.URL + "/signup", user);
}
signIn(user) {
return this.http.post<any>(this.URL + "/signin", user);
}
log(user) {
return this.http.post<any>(this.URL + "/login", user);
}
loggedIn() {
return !!localStorage.getItem("token");
}
getToken() {
return localStorage.getItem("token");
}
logout() {
localStorage.removeItem("token");
this.router.navigate(["/login"])
}
}
当我使用我的应用程序并尝试检查代码时,从后端大小会显示此问题:
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25607) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
你知道为什么吗
另外,我想通过这里问一下,为了保护我的应用程序,我正在使用guard,你知道更多保护它的技术吗
谢谢大家! 节点不会为异步代码引发的未捕获错误提供太多跟踪。如果您使用的是异步承诺样式,那么您希望在承诺上有一个
.catch()
,以捕获错误,以便查看和处理错误。对于async/await,您需要将可疑代码包装在try/catch块中
我建议将中间件主体包装在try…catch
中,以便捕获并报告任何未处理的中间件错误,同时向客户端发送有意义的响应(例如500错误)。例如:
router.post(“/signin”),异步(req,res)=>{
试一试{
const{email,password}=req.body;
const user=await user.findOne({email})
如果(!user)返回res.status(401).send(“电子邮件不存在/Mis cojones morenillos”)
如果(user.password!==password)返回res.status(401).send(“错误密码”);
const-token=jwt.sign({u-id:user.\u-id},“secretKey”);
res.status(200).json({token});
}
捕捉(错误){
console.log(err);//或者以您希望的方式记录它以查看发生了什么
资源状态(500)。发送(错误);
}
});
通常,如果某个东西在异步代码中可能有错误,那么该错误应该被某个东西捕获。否则,随着代码库的增长,追踪这些神秘的、无跟踪的错误可能会成为一场噩梦。非常感谢!这样做,我已经能够检测到问题!
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25607) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.