MongoDB 데이터베이스에서 데이터를 읽고 저장하는 REST API가 있습니다. 내가 사용하는 응용 프로그램은 양식을 검색하고 그 양식에서 개체 (작업)를 만든 다음 DB에 저장합니다. 양식 후, 나는 클릭 이벤트가 내 컨트롤러의 저장 기능을 트리거 한 다음 다른 URL로 리디렉션하는 버튼이 있습니다.클릭 후 AngularJS 방향 전환
버튼을 클릭하면 작업이 DB에 잘 추가되었지만 응용 프로그램이 작동하지 않으며 리디렉션이 호출되지 않는다고 말합니다. 그러나 응용 프로그램을 다시로드하면 새로운 "작업"이 DB에 잘 추가되었음을 알 수 있습니다. 이게 뭐가 잘못 됐니 ??? 감사 !
샘플 HTML (옥) 코드 : 각 모듈
button.btn.btn-large.btn-primary(type='submit', ng:click="save()") Create
컨트롤러 :
var myApp = angular.module('appProfile', ['ngResource']);
myApp.factory('myJobs',['$resource', function($resource) {
return $resource('/api/allMyPostedJobs',
{},
{
save: {
method: 'POST'
}
});
}]);
: 각 모듈
function myJobOfferListCtrl($scope, $location, myJobs) {
$scope.save = function() {
var newJob = new myJobs($scope.job);
newJob.$save(function(err) {
if(err)
console.log('Impossible to create new job');
else {
console.log('Ready to redirect');
$location.path('/offers');
}
});
};
}
구성 여기
내 코드내 nodejs 응용 프로그램에서 라우팅 :
app.post('/job', pass.ensureAuthenticated, jobOffers_routes.create);
그리고 내 REST API를 마지막으로 컨트롤러는 :
exports.create = function(req, res) {
var user = req.user;
var job = new Job({ user: user,
title: req.body.title,
description: req.body.description,
salary: req.body.salary,
dueDate: new Date(req.body.dueDate),
category: req.body.category});
job.save(function(err) {
if(err) {
console.log(err);
res.redirect('/home');
}
else {
console.log('New job for user: ' + user.username + " has been posted."); //<--- Message displayed in the log
//res.redirect('/offers'); //<---- triggered but never render
res.send(JSON.stringify(job));
}
});
};
"작업"(vs api/allMyPostedJobs) 끝점에 대한 POST가 호출 되었습니까? 그렇지 않다면 당신의 코드가 db에 어떤 것을 저장하는지조차 확신하지 못합니다. – angulord
예, 로그에 다음과 같이 표시됩니다. 사용자 XXX의 신규 작업이 게시되었습니다. 그리고 서버 응용 프로그램을 다시 시작할 때 DB에 게시물이 있습니다 ... – GuillaumeA
$ location.path()에 의한 리디렉션 직전에 발생할 수있는 '리디렉션 할 준비가되었습니다'라는 메시지가 로그에 표시되지 않습니다. . – GuillaumeA