2016-10-01 27 views
0

我現在是編碼的新手,所以如果提供的材料不夠清晰,請耐心等待,我會盡我所能。用ng-model定義對象的mongoose模式數組

這裏的問題

{ 
    email: 'asdf', 
    password: 'asdf', 
    userlists: { 
     list1: [ [Object], [Object], [Object] ] 
    } 
} 

我想要的終端送我回對象作爲字符串數組。

我試着去使用這個模式,

user.js的

var mongoose = require('mongoose'); 
module.exports = mongoose.model('User', { 
    email: String, 
    password: String, 
    userlists: { 
     list1: {type:{ sublist1: String }}, 
     list2: {type:{ sublist2: String }} 
    } 
}); 

我已經盡力了userlists設置爲對象的數組,這個返回相同的事與願違的結果,以及作爲列表1列表2的空數組,如果他們不填充。

我希望用戶使用我的以下$http.post方法和ng-model,並且我使用ngTagsInput指令讓用戶在註冊時選擇預製「標籤」。

註冊-controller.js

$scope.createUser = function(){ 
    console.log($scope.newUser); 
    $http.post('api/user/signup', $scope.newUser) 
    .success(function(response){ 

    }) 
    .error(function(error){ 
     console.log(error); 
    }) 
}; 

signup.html

<button ng-click="createUser()">Submit</button> 

    <hr> 
     <div> 
     <tags-input ng-model="newUser.userlists.list1" display-property="sublist1"> 
      <auto-complete  source="tagsLoadedFromNgTagsInput($query)" 
          min-length="1" 
          load-on-focus="true"  
          load-on-empty="true"> 
      </auto-complete> 
     </tags-input> 
     </div> 

這裏的結果執行時,我從服務器端獲取。 我希望數組內的對象顯示爲字符串,由上面的signup.html中的輸入設置。

{ 
    email: 'asdf', 
    password: 'asdf', 
    userlists: { 
     list1: [ [Object], [Object], [Object] ] 
    } 
} 

下面是蒙戈外殼登錄db.users.find().pretty()當結果:

{ 
    "_id" : ObjectId("57efd2dbdcb311107b4830b2"), 
    "email" : "asdf", 
    "password" : "asdf", 
    "userlists" : { 
     "list1" : [ 
      { 
       "sublist1" : "sometag1", 
       "_id" : "57ed472b0c868aafab696b61" 
      }, 
      { 
       "sublist1" : "sometag2", 
       "_id" : "57ed472b0c868aafab696b62" 
      }, 
      { 
       "sublist1" : "sometag3", 
       "_id" : "57ed472b0c868aafab696b63" 
      } 
     ] 
    }, 
    "__v" : 0 
} 

有誰知道我在做什麼錯在這裏?

回答

1

console.log函數在顯示對象之前包裝對象。爲確保顯示所有數據,您必須在調用控制檯之前使用JSON.stringify將其轉換爲字符串。

// Your object 
 
var obj = {some: {nested: {object: {definition: 'hello world!'}}}}; 
 

 
// Print as a single line 
 
console.log(JSON.stringify(obj)); 
 

 
// Print with 2 spaces identation 
 
console.log(JSON.stringify(obj, null, 2));

+0

非常感謝!工程就像一個魅力,使總體感覺:) – Dennis

0

使用ng-repeat,因爲list1是一個數組。

<tags-input ng-repeat="list in newUser.userlists.list1"> 
       <div>{{list.sublist1}}</div> 
</tags-input> 
+0

林不知道我理解不順利載入我的標籤在前臺,有IM的問題越來越在服務器端控制檯的正確輸出答案,但IM。 感謝您的答覆,我欣賞它! – Dennis

+0

使用'console.log(Object.userlists.list1)' –

相關問題