VueJs: Error - Unable to retrieve the length of an undefined property

I've been diligently following a Vue tutorial. Here's the snippet of code that I'm using to enforce a character limit on a text area:

<form action="" class="create-twoot" @submit.prevent="createnewTwoot">
   <label for="new-twoot">New Twoot</label>
   <p>{{char_limit}}/180</p>
   <textarea name="" id="new-twoot" cols="30" rows="4" v-model="newTwootcontent"></textarea>
   <br/>
   <label for="newTwootType">Twoot Type</label>
   <select name="TwootType" id="newTwootType" v-model="twootType">
      <option :value="option.name" v-for="(option,index)  in twootTypes" :key="index">
         {{option.value}}
      </option>
   </select>
   <br/>
   <button>Tweet</button>
</form>

JS CODE

export default {
  name: 'Userprofile',
  newTwootcontent: '',
  twootType: 'instant',
  
    computed:{
    char_limit(){
      return this.newTwootcontent.length;
    },
    fullname(){
      return `${this.users.fname} ${this.users.lname}`;
    }
  },
}

While most parts of my project are functioning smoothly, there seems to be an issue with the char_limit function resulting in the following error:

(I haven't included data, methods, etc. as they are working fine)

Cannot read property 'length' of undefined
    at Proxy.char_limit (Userprofile.vue?5045:102)

Any suggestions on what might be causing this problem?

Answer №1

To correctly define the variables within the data section, follow this structure:

export default {
  name: 'Profile',
  data() {
    return {
      newPostContent: '',
      postType: 'regular',
    };
  },
  computed: {
    characterLength() {
      return this.newPostContent.length;
    },
    fullName() {
      return `${this.users.firstName} ${this.users.lastName}`;
    }
  }
}

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

What is the best way to access event.target as an object in Angular 2?

Apologies for my limited English proficiency. . I am trying to write code that will call myFunction() when the user clicks anywhere except on an element with the class .do-not-click-here. Here is the code I have written: document.addEventListener(' ...

Are multiple instances of ajax being executed within the modal?

Currently, I am working on a feature that requires an Ajax request. To begin with, the process involves opening a modal by clicking a button with the class '.open-email-modal'. Within this modal, there are two input fields to enter registration d ...

What is the purpose of mapping through Object.keys(this) and accessing each property using this[key]?

After reviewing this method, I can't help but wonder why it uses Object.keys(this).map(key => (this as any)[key]). Is there any reason why Object.keys(this).indexOf(type) !== -1 wouldn't work just as well? /** * Checks if validation type is ...

Stopping Vue from endlessly looping

There are three fields to consider: hour, minutes, and total. If the hour or minutes change, I want to calculate the total. If the total is changed, I want to calculate the corresponding minutes and hours. For example: 1h 30minutes = 1.5 Total 2.25 To ...

Clicking on a row in a jQuery table will also capture the table headers

In my Vue application, I have a function that triggers when a table row is clicked. Here is the code snippet: $("#connectionsTable tr").click(function () { var list = []; var $row = $(this).closest("tr"), $tds = $row.find("td"); // console.log($td ...

Is it possible to develop a chat application utilizing only react, node js, and mongoose? Would socket.io be necessary for this project?

As I work on developing a straightforward chat application using react and node js, the use of sockets for establishing server-client connections raises questions for me. Given that react automatically re-renders the page upon state changes, it seems fea ...

Select just one picture for emphasis

I have a collection of images on display and I am looking to make edits to specific image details. My goal is to be able to click on an image to highlight it, and then use the edit button to make changes. Currently, the functionality is in place, but I&a ...

Issue with loading partial view in CodeIgniter using AJAX

My ajax function is working when trying to load a specific view (createClass.php), but it isn't functioning for other views like classAction.php. script: //--------------this works--------------- $(function(){ $(".classloader").click(function( ...

Using the Vue Class Component syntax in conjunction with TypeScript

I found an example on https://github.com/vuejs/vue-class-component that uses the new component syntax in vuejs3, but I'm trying to use TypeScript directly instead of babel. Here is what I attempted: <script lang="ts"> import Vue from "vue" impo ...

Using Javascript and Node.js to implement AI: Repeating a word from an array by using matching techniques

Seeking assistance with an AI project involving javascript and Node.js. I have set up hardcoded questions and answers, allowing users to modify the AI's responses. The majority of the javascript code has been implemented on the server-side. I am wor ...

Difficulties linking Dropdown choices with VueJs2

I am facing an issue with my dropdown list that contains document types. I want the selected type to be stored in a data property, specifically the type.name, so that I can use it in a child component. However, for some reason, my selection is not being sa ...

Using jQuery's Ajax method to send data from PHP script

I'm currently working on the following code snippet for my view: <b><input type="text" id="pagibigno" onclick="window.location.assign('#')"/></b> <div id="pagibig_form"> <div class="err" id="add_err"></d ...

I'm interested in exploring different database implementation patterns in JavaScript. What kinds of approaches can I consider?

As someone who is relatively new to exploring JavaScript, I have been immersed in experimenting with a node test app and MongoDB. I am eager to delve into the database aspect of the app but finding myself uncertain about the most commonly used patterns in ...

Checkbox remains selected even after navigating back

I am currently working on a code that involves using checkboxes. When I click on them, the checkbox value is appended to the URL with a hash. However, when I go back or press the back button, the URL changes but the checkboxes remain checked. Below is the ...

When using Nuxt JS and Jest, a warning message may appear stating "[Vue warn]: Invalid Component definition" when importing an SVG file

I am facing a unique issue only in my Jest unit test where an error occurs when I import an SVG into the component being tested: console.error node_modules/vue/dist/vue.common.dev.js:630 [Vue warn]: Invalid Component definition: found in -- ...

Why is req.app.post(...)create_event not functioning as expected?

Currently, I'm utilizing axios, node.js, express.js, and SQL to send a post request to my database. However, each time I attempt to do so, I encounter an error message stating: "TypeError: req.app.post(...).create_event is not a function". I've b ...

Refresh an Angular page automatically

Having a small issue in my angular application. The problem arises on the first page where I display a table listing all employees along with a "Create New Employee" button that opens a form for adding a new employee. However, after submitting the form and ...

Dynamic parallax effect with scroll wheel on WebKit

Currently experiencing some challenges with parallaxing backgrounds. I developed a website for an event organized by my friends, featuring multiple background images that shift between content sections to create a parallax effect. I have implemented code t ...

Having trouble with Knockout Js Array.removeAll() function not functioning as expected?

let values = ko.observableArray([....]); values.removeAll(); The scenario involves 'values' holding selected options from dynamically generated dropdowns using knockout. The goal is to clear all user selections. Unfortunately, the provided code ...

Associate the right-click action with Angular Material

I'm currently working with Angular Material. My goal is to develop a directive that allows me to trigger a right-click event on an element. This is what I have attempted so far: JavaScript: app.directive('rightClick', ["$parse", function ...