2016-10-01 55 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)' –

相关问题