gpt4 book ai didi

javascript - 使用 AngularJS 和 Node 更新 mongodb

转载 作者:行者123 更新时间:2023-12-03 05:32:23 26 4
gpt4 key购买 nike

我需要在前端使用nodejs和angularjs更新我的mongodb。我有下面的代码,但我收到类似“TypeError: Cannot read property 'put' of undefined

”的错误

我的 angularjs Controller :

myApp.controller('userController', ['$scope', '$resource', 'AuthService','iden','$http', function ($scope, $resource, AuthService,iden,$http) {

console.log(usersOnline);
var Meetup = $resource('/api/user', {},{
query: {method: 'get', isArray: true}
});

$scope.users = [];
$scope.userss = [];
$scope.text='mikyas';
Meetup.query({text: usersOnline}).$promise.then(function (results) {
$scope.users = results;
}, function(error) {
// console.log(error);
$scope.meetups = [];
});
console.log(usersOnline);


function getUser(iden,$http) {
//return promise here
var Users = $resource('/api/users', {},{
query: {method: 'get', isArray: true}
});

$scope.usersOnline='a';
return Users.query({username: usersOnline}).$promise
//other code as is

}

$scope.id='cpasgrave';
$scope.lol=getUser();
$scope.lol.then(function(user,$http){
console.log(user[0]._id);
iden=user[0]._id;
$scope.userss = user;
console.log(iden);

$http.put('/api/updateUser' + user[0]._id, user[0]);
});
console.log(iden);


}]);

以及我在服务器端的 api :*

module.exports.updateUser = function (req, res) {
var id = req.body.id;
User.findById(id, function(err, user) {
if (err) throw err;

// change the users location
user.auto = 'true';

// save the user
user.save(function(err) {
if (err) throw err;

console.log('User successfully updated!');
});

});
}
`

最佳答案

删除 $scope.lol.then(function(user,$http) 函数中的 $http 参数。

$scope.lol.then(function(user){
console.log(user[0]._id);
iden=user[0]._id;
$scope.userss = user;
console.log(iden);

$http.put('/api/updateUser' + user[0]._id, user[0]);
});

此外,如果您定义了 getUser (iden, $http),请确保在调用它时输入正确的参数...

$scope.id='cpasgrave';
$scope.lol=getUser(iden, $http);

关于javascript - 使用 AngularJS 和 Node 更新 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40886103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com