MEAN在这里堆叠新手。我创建了一个简单的应用程序,将名称和年龄作为练习。现在我被困在我必须将数组值推入我的mongoose数组的部分。
这是我的架构:
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);
});
}
});
这里&#39; sa Plunkr我的应用程序的样子,SO question我用作参考,因为它最接近我的应用程序试图做。
在people.peoplelist.push(peopleObj)
行,我收到"Cannot read property 'push' of undefined.
错误。这是否意味着我没有从mongoose正确获取我的数组模式?我该如何解决这个问题?
非常感谢任何回复。
一如既往地感谢您的指导。
答案 0 :(得分:0)
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
答案 1 :(得分:0)
最后在this post.的帮助下解决了这个问题。我对我的架构和客户端控制器进行了一些更改,以使其工作。
这就是我的所作所为:
我的新架构:
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);
});
}
});