使用Angular打印出MongoDB集合文档

时间:2017-11-21 14:50:47

标签: node.js mongodb angular

我从NodeJS,Angular和MongoDB制作基本登录系统。

我希望我的系统会在注册或成功登录后显示有关用户的信息(用户名和年龄)。但我遇到了问题。

当我注册成功时,只显示用户的年龄不显示用户名,而是当我登录时,它只显示用户名而不显示年龄。

我会展示一些代码,我觉得有问题:

Server.js

 // create new user object
var user = new User();
user.username = req.body.user.username;
user.age = req.body.user.age;
user.password = req.body.user.password;

user.js的

 //grab the packages that we need for the user model
var mongoose = require ('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

//user schema
var UserSchema = new Schema({
    username: {type: String, required:true, index: {unique:true}},
    age: {type: String, required:true, index: {unique:true}},
    faceId: {type: String, required:false},
    password: {type: String, required:true, select: false},
});


//hash the password before the user is saved
UserSchema.pre('save', function(next){
    var user = this;

    //hash the password only if the password has been changed or user is new
    if(!user.isModified('password')) return next();

    //generate the salt
    bcrypt.hash(user.password, null, null, function(err, hash){
        if (err) return next(err);

        //change the password to the hashed version
        user.password = hash;
        next();
    });
});

//method to compare a given password with the database hash
UserSchema.methods.comparePassword = function(password) {
    var user = this;
    return bcrypt.compareSync(password, user.password);
};

//return the model
module.exports = mongoose.model('User', UserSchema);

注册组件

// post user data and image url
this.userRestService.register({
    user: this.user,
    image: this.imageUrl
  }).subscribe(data => {
      var token = data.token;
      this.auth.setToken(token);
      this.auth.setUser({ username: this.user.username });
      this.auth.setUser({ age: this.user.age });
      this.router.navigate(['/home']);

我打印出来:

<div *ngIf="user">
<p>
  {{greeting}} {{user.room}} {{user.age}}      
</p>
</div>

登录组件:

import { Component, ViewChild, OnInit, Inject, ChangeDetectorRef } from '@angular/core';
import { Http } from '@angular/http';
import { UserRestService } from '../../services/user-rest.service';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  imageUrl;
  username;
  age;
  password;
  loginMethod: string = 'camera';

  constructor(private router: Router, private userRestService: UserRestService, private auth: AuthService, private ref: ChangeDetectorRef) { }

  ngOnInit() {}

  // login method
  login() {
    // set params: password or imageUrl, depending on chosen loginMethod
    var params: any = {
      username: this.username
    };
    if(this.loginMethod === 'password') {
      params.password = this.password;
    }
    else {
      params.image = this.imageUrl;
    }

    // perform http call
    this.userRestService.login( {
      username: this.username,
      password: this.password,
      image: this.imageUrl
    })
      .subscribe( data => {
          // on success set token and user data
          var token = data.token;
          this.auth.setToken(token);
          this.auth.setUser({username: this.username, age: this.age});
          //this.auth.setUser({room: this.room});


          // navigate to home page
          this.router.navigate(['/home']);
        });
  }

  // image changed handler for embedded components (image picker, camera snapshot)
  imageChanged(data) {
    this.imageUrl = data;
    this.ref.detectChanges();
  }

}

1 个答案:

答案 0 :(得分:0)

在这部分:

this.auth.setUser({username: this.user.username});
this.auth.setUser({age: this.user.age});

您要为用户设置两次:一次只使用用户名,一次只使用年龄。

您可能只需要设置一次用户对象,并使用正确的对象作为setUser()的参数:

this.auth.setUser({username: this.user.username, age: this.user.age});

登录部分可能存在相同的错误。