【问题标题】:Defining a mongoose schema array of objects with ng-model使用 ng-model 定义对象的 mongoose 模式数组
【发布时间】:2017-02-10 00:38:35
【问题描述】:

我现在是编码新手,如果提供的材料不够清楚,请耐心等待,我会尽力而为。

问题来了

{ 
    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 设置为对象数组,这会返回相同的不需要的结果,以及 list1list2 的空数组如果没有填写。

我希望用户将我的以下$http.post 方法与ng-model 一起使用,并且我正在使用ngTagsInput 指令让用户在注册时选择预制的“标签”。

signup-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] ] 
    } 
}

这是在 mongo shell 中记录 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
}

有谁知道我在这里做错了什么?

【问题讨论】:

    标签: angularjs node.js mongodb mongoose ng-tags-input


    【解决方案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));

    【讨论】:

    • 非常感谢!像魅力一样工作,完全有意义:)
    【解决方案2】:

    使用 ng-repeat 因为 list1 是一个数组。

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

    【讨论】:

    • 我不确定我是否理解答案,但我在前端加载我的标签时没有问题,我在服务器端控制台中获取正确输出时遇到问题。感谢您的回答,我很感激!
    • 使用console.log(Object.userlists.list1)
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2020-06-24
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多