Search code examples
angularjsnode.jsmongodbmongooseng-tags-input

Defining a mongoose schema array of objects with ng-model


I'm a rookie at coding at the moment so please have patience if the material provided is not clear enough, I'll do my best.

Here's the problem

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

I want the terminal to send me back the array of objects as strings.

Im trying to use this schema,

user.js

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

I've tried to set the userlists to an array of objects, this returns the same unwanted result, as well as empty arrays of the list1 and list2 if they are not filled in.

I want the user to use my following $http.post method together with ng-model, and I'm using ngTagsInput directive to let the user select premade "tags" when signing up.

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>

Here's the result I get from the server terminal when executed. I want the objects inside the array to be displayed as strings, set by the input in signup.html above.

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

Here's the result when logging db.users.find().pretty() in the mongo shell:

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

Does anyone know what I'm doing wrong here?


Solution

  • The console.log function wraps the objects before displaying it. For ensuring all data is displayed you must convert it to a string using JSON.stringify before calling the console.

    // 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));