Unable to reach the array file

Having trouble accessing the elements of the file array

  const p_page = postP.new_pages;
  const p_page_o = postP.new_pages_order;
  const p_page_o_len = p_page_o.length;
  if (p_page_o_len > 0) {
    for (let i = 0; i < p_page_o_len; i++) {
      console.log(p_page);  //Array with files inside it displayed
      console.log(p_page[0]);  //displays undefined 
      console.log(p_page[i]);  //also displays undefined

      formData.append("pages[]", p_page[i]);
      formData.append("pages_order[]", p_page_o[i]);
    }
  }

When attempting to log them:

https://i.sstatic.net/MgSAA.png

Edit: Essentially, I am adding image files to an array and then combining them in a FormData before sending them to the backend using a loop. While I can see the p_page logged in the console, I am unable to access any child element in the array as they are all showing as undefined. Apologies for any confusion in my explanation.

Answer №1

For some reason, this code did the trick:

  const p_page_o_len = postP.new_pages_order.length;
  for (let i = 0; i < p_page_o_len; i++) {

    const page = postP.new_pages[i];
    formData.append("pages[]", page);

    const order = postP.new_pages_order[i];
    formData.append("pages_order[]", order);

  }

I want to express my gratitude to everyone and apologize for the poor explanation. I still can't quite figure out what was wrong with it, but at least it's working for me now.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges regarding placement and z-index are causing issues

Currently, I am attempting to make the menu overlap the content on my webpage. However, I am facing an issue where it moves the content box instead of overlapping it. I have already experimented with the position: relative concept, but unfortunately, the ...

How can I place a div using pixel positioning while still allowing it to occupy space as if it were absolutely positioned?

I successfully created an slds path element with multiple steps. I want to display additional subtext information below each current step, but there might not always be subtext for every step. To achieve this, I created an absolute positioned div containi ...

Convert form data into a JSON object using Vue.js

I'm attempting to generate a JSON object from the submitted form data. Fortunately, I've accomplished this using a variable. However, is there an alternative approach for creating a JSON object? Form <form @submit.prevent="submit"& ...

Passport, Solution for Renewing Expired Tokens

Currently, I am utilizing Laravel version 5.8, VueJS, and Passport version 7.4 for handling Authentication in my project. Below you can find the code snippet for my login function: public function login(Request $request) { $validator = Valid ...

Can angular and grunt support multiple run blocks simultaneously?

Currently, I am configuring $httpBackend to simulate fake API routes while our team of API developers is building them out. However, I am facing an issue where I have to place all the $httpBackend configurations within my run block. This leads to a situa ...

Set iframe scroll to match the height of the user's screen resolution

Can the height of an iframe scroll be adjusted to match the size of the screen (browser)? Essentially replacing the default browser scroll with the iframe scroll. Here's what I'm looking to achieve: Currently, my browser scroll is disabled: < ...

'this' in Arrow functions does not have a reference to the calling context

Something seems off about the context in this code. I've left comments to describe my issue below: const cat = { //arrow function meow: () => { console.log(this); }, makeMeow(){ // Why does 'this' refer ...

Fs claims that a file does not exist, however, it actually does

Currently, I am facing an issue with a file that contains the following code: const cmds = JSON.parse(fs.readFileSync('./cmds.json')); The file where this code is running is located in the same folder as `cmds.json`, but for some reason, it cann ...

Expanding a list of object arrays through iteration: A step-by-step guide

// Initializing with one object let posi_val=[{top: '3px', left: '2px'}]; // Adding another object in the loop for (let i = 1; i < n; i++) { posi_val.push({ top: `${posi_val[i - 1].top.slice(0, -2) * 2}px`, l ...

Utilizing vue-i18n within Class-based Components in Vue 3

Currently, I am in the process of migrating from vue2 to vue3 and using the "Class Style Component". I have been utilizing vue-i18n. Will I be able to continue using vue-i18n with Vue3? Here is a snippet from my package.json: "dependencies": ...

Using JSON / jQuery: How to Handle XML Data in the Success Function of an AJAX Post Request

Greetings! I am currently performing an ajax post through a JSP. The JSON data is being sent in string format (parsed using parseJSON, then converted back to a string using JSON stringify). The post operation functions correctly. However, my query lies in ...

The Unicode range [uXXXX] in Regex is not recognized during the AJAX request

I am currently facing an issue with removing control characters (e.g. \u0005) from the ajax response string. I have implemented a regular expression for this purpose: msg = msg.replace(/[\x00-\x1F\x7F-\x9F]/g, ''); Inter ...

What are the differences between Sitecore Analytics and Adobe Analytics? How do they each utilize JavaScript

I have limited knowledge about Sitecore Analytics (with MongoDB) and I am curious if there exists a Javascript API that can be utilized for non-Sitecore websites. If so, could you please direct me to the relevant documentation? Additionally, any insights ...

`The header navigation is not responding to window resizing functionality`

I am currently developing a header navigation that consists of a logo on the left side, a profile icon on the right side, and some navigation links in the middle. A left slide menu has been implemented to trigger when the window width is less than 700px. ...

When attempting to call a script function in PHP and expecting a return value, an error was encountered: "Uncaught SyntaxError

My functions are working fine: <script> function createCookie(name,value,sec) { if (sec) { console.log('createCookie: '+name+', '+value+', '+sec); var date = new Date(); date.setTime(date.getTime()+(sec*1000 ...

Collision detection in Physisjs with Three.js PointerLockControls is an essential feature for creating

I am currently working on a project using Three.js with PointerLockControls. My goal is to implement collision detection for the player in order to enhance the overall experience. To achieve this, I have created a new Physijs cylinder object and passed it ...

Performing a dynamic animation by toggling the class of an element with "classList.toggle" in JavaScript

I have been attempting to incorporate a fade animation using CSS when altering the class of the input and label elements within a form by utilizing the classList.toggle method in JavaScript. I'm puzzled as to why my code isn't producing the desir ...

Creating an automatic image carousel using a combination of Jquery, Javascript, and CSS

I have been working on creating a slider using jQuery and javascript, but I am facing some issues with the autoplay functionality and the next-previous buttons. This slider is intended for displaying images as a title indicates. Can anyone assist me with i ...

Confidential data in Module Design

I'm curious about the concept of private variables in JavaScript. How is it that I can still access a private variable through a public method and redefine the properties of the module? For example: var aModule = (function() { var privateVar = 1 ...

Tips for utilizing a shared controller in an Angular Material popup dialogue

Within my project, Angular Material is being used for various dialogs, primarily for alert purposes. However, I now find myself in need of a more complex dialog. The example provided on the Angular Material site showcases how to create a dialog: function ...