I've been encountering random issues with certain parts of my code.
This object is initialized within an angular controller
.
this.tData = {
'questions':[],
'typeQuestion':[],
'category':[],
'dName':this.dName,
'tCodigo':this.tCodigo}
Subsequently, I retrieve data from other functions and populate the respective fields as follows:
this.tData.questions.push(this.idQuestion) // sourced from frontend ng-model
this.tData.typeQuestion.push(this.typeQuest) // sourced from frontend ng-model
this.tData.category.push(this.idCategory)// sourced from frontend ng-model
At this point, my object is correctly constructed. When I perform a console.log(this.tData)
, the object appears to be fine. However, upon passing it to the backend within this function of the angular service
.
this.updateStuff = function(codStuff,tData){
return $http.put('/updateStuff' + codStuff,tData)}
The object received by the backend, as shown by console.log(params)
, is:
{
questions:['exampleId'],
typeQuestion:['exampleData'],
category:[], // ISSUE OCCURS HERE
dName:'exampleName',
tCodigo:'exampleCod'}
As seen, category:[]
is empty in the backend, despite displaying the correct data when using console.log(tData)
in the angular service before transmission. This issue has occurred on three separate instances.
Why do some arrays successfully reach the backend while others do not?
I have attempted various solutions, but each time, one item of the transmitted object ends up empty.
If you require more specific code snippets, please let me know in the comments.
Updates
Here is where I add category information in the controller:
this.getCategoryByName = function(){
this.bName = document.getElementById('seCategory').value;
Category.getCategoryByName(this.bName).then((result)=>{
this.idCategory = result.data.data._id; // obtains category ID
this.tData.category.push(this.idCategory);
})
}
2
This is how I invoke my functions in the frontend:
<button class="btn btn-primary" ng-click="ctController.getCategoryByName(); ctController.updateTest();" > Update </button>
Below is the code for the updateTest() function:
this.updateTest = function(){
Test.updateTest(this.codTest,this.tData).then(result=>{})
}
This method calls the angular service
updateStuff
SOLVED
The issue was resolved by implementing a chain promise in the getCategoryByName method and nesting the updateTest() method within it, similar to the suggestion by @T.J. Crowder. This adjustment provided the necessary solution.