在Login.controller中传递_id参数时遇到问题

时间:2016-07-30 14:10:39

标签: angularjs mongodb

我正在使用angular-fullstack-generator。

我在将_id参数传递给某个函数时遇到了麻烦。 我想要实现的是在我创建了一个用户后,我想在模式中创建另一个数据引用用户的id

因此这是我的代码

'use strict';

class SignupVendorController {
  //end-non-standard

  constructor(Auth, $state, $http) {
      this.Auth = Auth;
      this.$state = $state;
      this.$http = $http;
      this.submitted = false;
    }
    //start-non-standard


  register(form) {
    this.submitted = true;

    if (form.$valid) {
      this.Auth.createUser({
          firstName: this.user.firstName,
          lastName: this.user.lastName,
          email: this.user.email,
          password: this.user.password,
          role: 'vendor'
        })
        .then( Auth => {

          this.vendor = Auth.getCurrentUser;
          this.createVendor(this.vendor._id);
          console.log('user is ' + this.vendor.firstName);
          console.log('vendor created');
          // Account created, redirect to home
          this.$state.go('main');
        })
        .catch(err => {
          err = err.data;
          this.errors = {};

          // Update validity of form fields that match the mongoose errors
          angular.forEach(err.errors, (error, field) => {
            form[field].$setValidity('mongoose', false);
            this.errors[field] = error.message;
          });
        });
    }
  }


  createVendor(id) {

          var vendor = {
            category: ['publishing'],
            _owner: id
          }

          this.$http.post('/api/vendors', vendor);

  }

}

angular.module('aApp')
  .controller('SignupVendorController', SignupVendorController);

我已经尝试了几种语法修改,但它只是在this.vendor上得到了未定义的变量。

像这样,例如

var vendor = Auth.getCurrentUser;
console.log('the user is ' + vendor.firstName);

代码无法读取_id或firstName。 任何形式的帮助将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

你能试试吗?:

this.vendor = Auth.getCurrentUser();
console.log('the user_id is ' + this.vendor._id);

在我的应用程序中,这是有效的。

相关问题