I'm looking to create a Vuex getter to retrieve data from the Google API documentation – can you help

Can someone help me figure out how to create a getter in Vuex store with flat data from the Google Docs API? My goal is to extract the textRun content and store it in an array because there will be multiple messages. Currently, I have hard coded this response in the state like this:

 state: {
    googleResponse: [
      {
        "body": {
          "content": [
            {
              "endIndex": 75,
              "paragraph": {
                "elements": [
                  {
                    "endIndex": 75,
                    "startIndex": 1,
                    "textRun": {
                      "content": "This is an ordinary paragraph. It is the first paragraph of the document",
                      "textStyle": {}
                    }
                  }
                ],
                "paragraphStyle": {
                  "direction": "LEFT_TO_RIGHT",
                  "namedStyleType": "NORMAL_TEXT"
                }
              },
              "startIndex": 1
            },
            {
              "endIndex": 102,
              "paragraph": {
                "elements": [
                  {
                    "endIndex": 102,
                    "startIndex": 75,
                    "textRun": {
                      "content": "Here's a level one heading",
                      "textStyle": {}
                    }
                  }
                ],
                "paragraphStyle": {
                  "direction": "LEFT_TO_RIGHT",
                  "headingId": "h.o1fkftgl5zwf",
                  "namedStyleType": "HEADING_1"
                }
              },
              "startIndex": 75
            },
          ]
        }
      }
    ],
} 

After that, I created a getter message and used the map function from lodash:

message: (state) => {
  let message = '';
  map(state.googleResponse, (element) => ({
    content: map(element.body.content, (content) => {
      map(content.paragraph.elements, (obj) => {
        message += get(obj, 'textRun', '')
      })
    })
  }))
}

However, when I check the message in Vuex, it shows as undefined. I want to have an array with textRun objects. What could be causing this issue?

Answer №1

Is it possible to store the message in an Array using this method? Feel free to structure your code like this:

let messageArray = state.googleResponse.map(
    item => item.body.content.map(
        itemContent => itemContent.paragraph.elements.map(
            itemElements => itemElements.textRun.content)))

Answer №2

Make sure to include the message variable in your getter method.

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

Tips for identifying the category of a hyperlink within my javascript code?

Hello, I am currently working on a script that involves AJAX functionality: function ajax(){ if (navigator.standalone) return; for (var i= document.links.length; i-->0;) { document.links[i].onclick= function() { var req= new ...

"Upon invoking the console log, an empty value is being returned when attempting to access the

Why is console.log returning a blank line when trying to retrieve the value of a text input variable? HTML <label for="subject">Subject</label> <input type="text" id=" ...

Refreshing express-session sessions

My goal is to enhance the user experience by retrieving their profile from the mongodb database when they visit the page, and then updating their session with this information. Currently, I am utilizing the following packages for managing sessions: - ex ...

Have the quotes within my markup been replaced with HTML entities?

Currently, I am working on a content page that uses a master page which includes a text box and a button. My goal is to have the button execute some JavaScript code before performing any other actions. At the moment, I am in the testing phase of this JavaS ...

The Material-UI table spills out of its designated container

My material-ui table is resizing and scrolling horizontally without issue, but the table element and its contents are overflowing off the right side of the page. Check out this gif to see the behavior: https://i.stack.imgur.com/lXuHj.jpg Additionally, her ...

Verifying Vuejs values post-click testing

<!-- custom template --> <div> <textarea v-model="someText">{{someText}}</textarea> <div v-if="hasError">Oops, an error occurred</div> <input v-on:click="store" type="submit" value="update" /> </div ...

What steps should be taken to fix the error "Warning: Encountered multiple children with the same key" in React.js?

I'm having trouble fetching and displaying data from an API. Whenever I attempt to show the data, I encounter the following error message: Alert: Found two children using the same key, [object Object]. Keys must be unique so that components can pro ...

Having trouble getting the Html onload function to work in Google Sheets App Script?

I'm working with google sheets and I'm in the process of creating a document to track employees who are currently out of the office. I have added a menu option that allows me to remove employee data, which triggers the opening of a sidebar contai ...

router.query is returning an empty object when using Next.js

Here is how my folders are organized: https://i.stack.imgur.com/TfBtv.png In addition, here is a snippet of my code: const router = useRouter(); const { id } = router.query; The issue I'm facing is that the id is returning {} instead of the actual ...

Unexpected issue with Ajax form submission when using the submitHandler function in Jquery

I am faced with the challenge of validating a form using the jQuery Validation Plugin and submitting data to a database without refreshing the page. Despite marking all fields in my form as required, the issue arises where empty fields are still submitted, ...

Eliminate any undefined data in the popup map of Javascript

I am working with JSON data that is being displayed in a pop-up on a map. In cases where there is missing data (Visibility), the word undefined appears in the pop-up. Is there a way to remove the undefined text so that it does not show up in the pop-up? ...

Designing a Curved Stepper Component in the Shape of an S

I've been attempting to create a custom stepper component with an S-curve shape using React, but so far I haven't been successful. I even tried utilizing the MUI Stepper component and experimented with pure HTML and CSS, but couldn't achieve ...

"Changing background color, incorporating hover effects, utilizing !important, and manipulating .css properties with

Encountered a dilemma. I devised a "tabs" feature illustrated in the following demo: http://jsfiddle.net/4FLCe/ The original intention was for the tab to change color to A when hovered over, and to color B when clicked on. However, as shown in the demo, ...

Tips for incorporating ajax to load a new webpage into a specific div container

I have a website with two pages. I want to load Page 2 into a div on Page 1 and then display Page 2 when clicked on. I'm attempting to implement the following code, but it doesn't seem to be working for me. As a beginner, I apologize if this is a ...

Bridging the Gap: Creating a Seamless Connection between Rails and Vue for an

I'm a bit confused about this. When working with Rails and erb files to construct page layout, 1) what scenarios call for the use of webpacker to incorporate Vue? From my understanding, a Vue application might need to make server requests som ...

Can a Nuxt project incorporate a Wordpress page?

Can a landing page created in Wordpress be integrated into a Nuxt.js project? ...

I encountered a ReferenceError stating that the variable "html" is not defined

Recently, I delved into the world of Node.js for the first time. During my attempt to connect my index.html file, an error message stating 'ReferenceError: html is not defined' popped up. Below is the code snippet from my index.js: var http = re ...

Tips for sorting through a multi-dimensional array of objects

I'm looking to filter this array based on the attributevalue. For example, if I search for blue color, I want all shirts in blue color to be displayed. And then if I further search for cotton fabric in blue color, I want all cotton products with blue ...

What is the most effective way to send URL strings to the server-side click handler in a jQuery loop using $.each method?

As I work with URL values in my client-side script, I aim to generate links that can transmit these URL values back to my server-side click handler upon clicking. The server-side code /logclick logs the timestamp and destination of the click for auditing p ...

Tips for iterating through an array of objects nested within other objects

Good afternoon everyone, I’m having trouble accessing the attributes of a JavaScript object array through AJAX. Can anyone spot where I might be going wrong? View image description This is my AJAX call: $("#cbx1").on("change", func ...