如何在 MEAN 的猫鼬模式中正确推送数组值

How to properly push array values in a mongoose schema in MEAN

这里是 MEAN 堆栈新手。我创建了一个简单的应用程序,将姓名和年龄作为练习。现在,我对必须将数组值推入猫鼬数组的部分感到困惑。

这是我的架构:

var schema = new mongoose.Schema({
    name: String,
    peoplelist : [{
        name: String,
        age: String
    }]
});

这是我的客户端控制器:

app.controller("IndexController", function ($scope, $resource) {
    var PeopleAPI = $resource('/api/post/');

    // This is used by ng-repeat
    $scope.inputfields = [{
        infoname: '',
        infoage: ''
    }];

    // This will be used when transferring details to mongoose schema
    $scope.arrayppl= {
        name: '',
        details: [$scope.inputfields]
    };

    // Adds new fields in the UI - adding friends is unlimited.
    $scope.addDetails = function () {
        $scope.inputfields.push({});
    }


    $scope.saveToDB = function () {
        var people = new PeopleAPI();

        // save name to main array (arrayppl)
        $scope.arrayppl["name"] = $scope.username;

        // save infoname and infoage to arraymen
        var len = $scope.inputfields.length;

        for (var i = 0; i <= len - 1; i++) {
            var tempArr = {};
            tempArr["infoname"] = $scope.inputfields[i].infoname;
            tempArr["infoage"] = $scope.inputfields[i].infoage;

            $scope.arraymen["details"].push(tempArr);
        }

        people.name = $scope.arrayppl["name"];

        var i = 0;
        angular.forEach($scope.arraymen["details"], function (item) {

            var peopleObj = {name: item[i].infoname, age: item[i].infoage};

            people.peoplelist.push(peopleObj); // <-- error gets triggered here

            i+=1;
        });

        people.$save(function(result){
           console.log(result);
        });
    }
});

这是我用作参考的 Plunkr of what my app looks like, and the SO question 因为它最接近我正在尝试做的事情。

people.peoplelist.push(peopleObj) 行,我收到 "Cannot read property 'push' of undefined. 错误。这是否意味着我没有从 mongoose 正确获取我的数组模式?我该如何解决这个问题?

如有任何回复,我们将不胜感激。

感谢您一如既往的指导。

var people = new PeopleAPI(); is like object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

您正试图推入对象。 push 只能工作数组。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

最终在 的帮助下解决了这个问题我对我的架构和 client-side 控制器做了一些更改以使其工作。

这是我所做的:

我的新架构:

var schema = new mongoose.Schema({
    name: String,
    peoplelist : []
});

我的新控制器:

app.controller("IndexController", function ($scope, $resource) {
    var PeopleAPI = $resource('/api/post/');

    $scope.inputfields = [{
        infoname: '',
        infoage: ''
        }];

    $scope.arraymen = {
        name: '',
        details: []
    };

    $scope.addDetails = function () {
        $scope.inputfields.push({});
    }

    $scope.saveToDB = function () {
        var people = new PeopleAPI();

        // save name to main array (arraymen)
        $scope.arraymen["name"] = $scope.username;

        // save infoname and infoage to arraymen
        //var len = Object.keys($scope.inputfields.infoname).length;
        var len = $scope.inputfields.length;

        for (var i = 0; i <= len - 1; i++) {
            var tempArr = {};
            tempArr["infoname"] = $scope.inputfields[i].infoname;
            tempArr["infoage"] = $scope.inputfields[i].infoage;

            //$scope.arraymen["details"].push(tempArr);
            $scope.arraymen["details"].push({
                "name": tempArr["infoname"],
                "age": tempArr["infoage"]
            });
        }


        people.name = $scope.arraymen["name"];
        //people.peoplelist = new Array();
        people.peoplelist = [];


        for (var i = 0; i <= $scope.arraymen["details"].length - 1; i++) {
            people.peoplelist.push({
                name: $scope.arraymen["details"][i].name,
                age: $scope.arraymen["details"][i].age
            })

        }

        people.$save(function (result) {
            console.log(result);
        });

    }
});