I have an angular controller that takes data from a form, manipulates the data and sends it to the server via $http
to be saved
The issue is that the code works fine sometimes i.e it returns the success function and doesn't work sometimes i.e it returns error code 0 but even when this happens the data is still being saved in the database even the server log shows that its returning success, why is this? The success function is called sometimes and sometimes the error one is called. What am I doing wrong?
The angular controller is:
app.controller("premiumAdvertsController",function($scope,ergastAPIservice,$routeParams,$location,$rootScope)
{
function uploadImage(input,success) {
console.log("yeh input hai ",input);
if ( input.files && input.files[0] ) {
var FR= new FileReader();
FR.onload = function(e) {
success(e.target.result);
};
FR.readAsDataURL( input.files[0] );
}
}
$scope.registerPremiumAdvertisement=function()
{
console.log("shakil k sath check!eeersss!");
console.log("hello");
var count=1;
var owner=$rootScope.currentUser.email;
var companyName=$scope.companyName;
var companySlogan=$scope.companySlogan;
var companyAddress=$scope.companyAddress;
var companyFacebookLink=$scope.companyFacebookLink;
var companyTwitterLink=$scope.companyTwitterLink;
var companyContactNumber=$scope.companyContactNumber;
var companySiteLink=$scope.companySiteLink;
var sliderText=$scope.imageText;
var category="premiumBrand";
var featuredOffer1Title=$scope.featuredOffer1Title;
var featuredoffer1Description=$scope.featuredoffer1Description;
var featuredoffer1Expiry=$scope.featuredoffer1Expiry;
var featuredOffer2Title=$scope.featuredOffer2Title;
var featuredoffer2Description=$scope.featuredoffer2Description;
var featuredoffer2Expiry=$scope.featuredoffer2Expiry;
var featuredOffer3Title=$scope.featuredOffer3Title;
var featuredoffer3Description=$scope.featuredoffer3Description;
var featuredoffer3Expiry=$scope.featuredoffer3Expiry;
var featuredOffer4Title=$scope.featuredOffer4Title;
var featuredoffer4Description=$scope.featuredoffer4Description;
var featuredoffer4Expiry=$scope.featuredoffer4Expiry;
console.log("hello");
var detailedOffer1Description=$scope.detailedOffer1Description;
var detailedOffer2Description=$scope.detailedOffer2Description;
var detailedOffer3Description=$scope.detailedOffer3Description;
var detailedOffer4Description=$scope.detailedOffer4Description;
console.log("hello");
var storeName=$scope.storeName;
var file1=document.getElementsByClassName("companyLogo")[0];
var file2=document.getElementsByClassName("picture2")[0];
var file3=document.getElementsByClassName("picture3")[0];
var file4=document.getElementsByClassName("picture4")[0];
var file5=document.getElementsByClassName("picture5")[0];
console.log("hello");
//var file6=document.getElementsByClassName("picture6")[0];
//var file7=document.getElementsByClassName("picture7")[0];
var f1,f2,f3,f4,f5;
var out=[f1,f2,f3,f4,f5];
var arr=[file1,file2,file3,file4,file5]
for (var i=0;i<arr.length;i++ )
{
console.log(i);
if( arr[i].files[0])
{
(function(ith){
uploadImage(arr[ith],function(result){
out[ith]=result;
count++;
console.log(count,"count");
if(count==6)
{
console.log("count hogya six!");
var featuredOffer1={offerTitle:featuredOffer1Title,offerDescription:featuredoffer1Description,offerExpiry:featuredoffer1Expiry};
var featuredOffer2={offerTitle:featuredOffer2Title,offerDescription:featuredoffer2Description,offerExpiry:featuredoffer2Expiry};
var featuredOffer3={offerTitle:featuredOffer3Title,offerDescription:featuredoffer3Description,offerExpiry:featuredoffer3Expiry};
var featuredOffer4={offerTitle:featuredOffer4Title,offerDescription:featuredoffer4Description,offerExpiry:featuredoffer4Expiry};
var featuredOffers=[];
featuredOffers.push(featuredOffer1);
featuredOffers.push(featuredOffer2);
featuredOffers.push(featuredOffer3);
featuredOffers.push(featuredOffer4);
var pages=[];
var pageCount=0;
var offerDescriptions=[];
var detailerOffer1={offerDescription:detailedOffer1Description,offerPicture:out[1]}
var detailerOffer2={offerDescription:detailedOffer2Description,offerPicture:out[2]}
var detailerOffer3={offerDescription:detailedOffer3Description,offerPicture:out[3]}
var detailerOffer4={offerDescription:detailedOffer4Description,offerPicture:out[4]}
offerDescriptions.push(detailerOffer1);
offerDescriptions.push(detailerOffer2);
offerDescriptions.push(detailerOffer3);
offerDescriptions.push(detailerOffer4);
var f={pageCount:pageCount,owner:owner,category:category,pages:pages,companyLogo:out[0],imageText:sliderText,companyName:companyName,companySlogan:companySlogan,companyAddress:companyAddress,companyContactNumber:companyContactNumber,companyFacebookLink:companyFacebookLink,companyTwitterLink:companyTwitterLink,companySiteLink:companySiteLink,featuredOffers:featuredOffers,offerDescriptions:offerDescriptions,premiumAdd:true};
console.log("f bangya!");
ergastAPIservice.saveHotBrand(f).then(function(res){
console.log("server pe chala gya data!");
if(res.success)
{
console.log("successfully got success from server !!");
$(".modalKaText").text("successfully uploaded");
$("#myModal").modal();
$location.path("/home");
}
else if(res.error)
{
console.log("got error from server!!!");
$(".modalKaText").text("There was some error");
$("#myModal").modal();
$location.reload()
}
else {
console.log("got nothing from server");
}
},function(res){
res=JSON.stringify(res);
console.log("error found!!",res);
})
}
})
})(i);
}
else
{
count++;
console.log(count,"count");
}
}
}
});
the code for the $http function (saveHotbrand) is :
ergastAPI.saveHotBrand=function(abc)
{
return $http({
method:'POST',
url:'/saveAdd',
data:abc
})
}
app.post('/saveAdd',function(req,res){
console.log("save add request!");
var collection=req.db.get("advertisements");
console.log("got database")
collection.insert(req.body,function (err,doc){
if(err)
{
console.log("sending error")
res.send({error:true});
}
else {
console.log("sending success")
res.send({success:true});
}
})
})
答案 0 :(得分:0)
Try this in the nodejs code:
app.post('/saveAdd',function(req,res){
var collection=req.db.get("advertisements",function(err){
collection.insert(req.body,function (err,doc){
if(err)
{
res.send({error:true});
}
else {
res.send({success:true});
}
})
})
})
I had faced a similar problem which was resolved by adding a callback function. You can see the question here.
答案 1 :(得分:0)
将您的eargast呼叫功能更改为此 ergastAPIservice.saveHotBrand(F).success(函数(RES){ }
然后它才会成功。
答案 2 :(得分:0)
您通过单击按钮提交表单并处理其单击事件以提交表单,无论何时执行此操作,您都需要从onCLick函数返回false。
答案 3 :(得分:0)
根据this页面,错误代码0实际上是成功的代码,它只是导致问题的if条件。包括对0
的检查,它应该有效。
答案 4 :(得分:0)
因此对于任何人来说,页面基本上都被重定向,所以我开始通过ng-click而不是ngsubmit来调用控制器功能,并且它可以工作。