2016-07-30 74 views
0

我正在使用angular-fullstack生成器。無法在Login.controller中傳遞_id參數

我在將_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或您的名字。 任何形式的幫助將不勝感激。 謝謝!

回答

0

你可以試試這個?:

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

在我的應用程序,這是工作。