【发布时间】:2017-05-28 12:18:19
【问题描述】:
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,以及我用作参考的SO question,因为它最接近我想要做的事情。
在people.peoplelist.push(peopleObj) 行,我收到"Cannot read property 'push' of undefined. 错误。这是否意味着我没有从 mongoose 正确获取我的数组模式?我该如何解决这个问题?
任何回复将不胜感激。
一如既往地感谢您的指导。
【问题讨论】:
标签: angularjs mongodb mongoose mean-stack