Vue file error "TypeError: Cannot read property 'content' of undefined"

I am writing a vue program imitating markdown

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=" initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>notebook</title>
        <!-- Icons & Stylesheets -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!--add the marked library:-->
    <script src="https://unpkg.com/marked"></script>
</body>
    <!--Notebook app-->
    <div id="notebook">
        <!-- sidebar-->
        <aside class="side-bar">
            <!--Toolbar-->
            <div class="toolbar">
                <!-- Add note button -->
                <button :title="addButtonTitle" @click="addNote"><i class="material-icons">add</i>Add note</button>
            </div>
            <div class="notes">
                <!--Note list here-->
                <div class="note" v-for="note in notes" @click="selectNote(note)">{{ note.title }}</div>
            </div>
            
        </aside>
        <!--Main pane-->
        <section class="main">
            <textarea v-model="selectedNote.content"></textarea>
        </section>
        <aside class="preview" v-html="notePreview">
        </aside> 
    </div>
    <!--some javascript -->
    <script src="script.js"></script>
</html>
new Vue({
    // css selector of the root DOM element
    el: "-sharpnotebook",
    // some data
    data () {
        return {
           content: localStorage.getItem("content") || "you can write in **markdown**", 
            // New! a note array
            notes: [],
            selectedId: localStorage.getItem("selected-id") || null,
        }
        
    },
    computed: {
        notePreview () {
            //markdown rendered to HTML
            return  this.selectedNote? marked(this.selectedNote.content):""
            // return  marked(this.content);
        },
        addButtonTitle () {
            return this.notes.length + "notes already"
        },
        selectedNote () {
            // we return the matching note with selectedId 
            return this.notes.find(note => note.id === this.selectedId) 
            //  this.selectedId = note.id
        },
    },
    watch: {
        // watching "content" data property
        content:
                {
                  handler:"saveNote",
                  deep: true,
                 selectedId(val, oldVal) {
                 localStorage.setItem("selected-id", val)
        },
        },
    },
    
    methods: {
        // Add a note with some default content and select it 
        addNote () {
            const time = Date.now()
            // Default new note 
            const note = {
                id: String(time),
                title: "New note" + (this.notes.length + 1),
                content: "**Hi!** This new note is using [markdown](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) for formatting!",
                created: time,
                favorite: false,
            }
            // Add to this list 
            this.notes.push(note)
        },
        selectNote (note) {
            this.selectedId = note.id
        },
        saveNote() {
            localStorage.setItem("content",this.content)
        },
    
    }})
    

but now TypeError: Cannot read property "content" of undefined "is always wrong." I don"t know what"s wrong.

May.01,2022

check for yourself where the value of xxx.content,xxx is the error caused by undefined


you see if printing content during the created declaration period is undefined, and I see if your Content depends on local storage


global search for content, which should be before naming or object value.


when there is no definition of this value before the declaration, what is written in your code is < textarea vMutel model = "selectedNote.content" > < / textarea >, that is, the value of the variable content, which is defined in the object seleectNote. Then you need to check whether this property in your object has a value, or if it has not been obtained, if there is no value. Or if you don't get it dynamically, you will tell undefined


where the value is not assigned or the value is undefined and then referenced to content through .content. The


selectedNote method defaults to undefined when the page is created, so when the textarea node binds data in both directions and cannot find selecteNote.content, it will report an error. Change it to the following and add a null value.
return this.notes.find (note = > note.id = this.selectedId) | |';
or, wrap the section and aside tags within the < template > tag, and add the condition VMI if = "selectedNote"


add a judgment statement to the output, and do not display

if content is null.
<section class="main" v-if="selectedNote">

<aside class="preview" v-if=selectedNote"  .....

Yes, one by one

Menu