Using Chance JS library in Vue.js

I'm facing a dilemma with my database entry views - one for songs and the other for genres. While trying to automatically fill input fields with random data, I noticed an issue when importing chance. It seems that importing chance in the genres file but not in the songs file causes the songs page to work fine, but the genres page fails.

Genre View:

<template>
  <div>
    <input type="text" id="genre-name" placeholder="Name" v-model="name" /><br />
    <input type="text" id="genre-country" placeholder="Country" v-model="country" /><br />
    <input type="text" id="genre-year" placeholder="Year" v-model="year" /><br />
    <button @click="addGenre" id="genre-button">Add Genre</button>
  </div>
</template>

<script>
import { requestOptions, base_url } from '@/utils/requestOptions';
//var chance = require('chance').Chance(); this works for both, when importing only in one file
import {chance} from "chance"; //<= this is the line I am talking about
export default {
  data() {
    return {
      name: chance.radio(),
      country: chance.country({ full: true }),
      year: chance.year()
    }
  },
  methods: {
    addGenre() {
      //...
    }
  }
}
</script>

Song View:

<template>
  <div>
    <input type="text" id="name" placeholder="Name" v-model="name" /><br />
    <input type="text" id="author" placeholder="Author" v-model="author" /><br />
    <input type="text" id="country" placeholder="Country" v-model="country" /><br />
    <input type="text" id="duration" placeholder="Duration" v-model="duration" /><br />
    <input type="text" id="views" placeholder="Views" v-model="views" /><br />
    <input type="text" id="genre" placeholder="Genre" v-model="genre" /><br />
    <button @click="addSong">Add Song</button>
  </div>
</template>

<script>
import { requestOptions, base_url } from '@/utils/requestOptions';
//this is working without importing chance
export default {
  data() {
    return {
      name: chance.word(),
      author: chance.last(),
      country: chance.country({ full: true }),
      duration: chance.minute(),
      views: chance.integer({ min: 0, max: 100000000 }),
      genre: chance.radio()
    }
  },
  methods: {
    addSong() {
      //...
    }
  }
}
</script>

The error message I am getting when I am opening the Genre View is:

TypeError: undefined is not an object (evaluating 'chance__WEBPACK_IMPORTED_MODULE_1__.chance.radio')

So I want to know why is it working on the Song View?
If I delete the import line, it will not work in any view.

Answer №1

I've been facing a problem and came up with a solution that worked for me:

import Random from "random";
var random = new Random();

random.generate({
  characters: "abcdefghijklmnopqrstuvwxyz1234567890",
  length: 5
});

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

Discover the best way to connect your Chrome bookmarks to a server through an extension using an API

I have embarked on creating a unique Chrome extension that will enable users to not only view, update, create, and delete Chrome bookmarks but also save and retrieve bookmarks through our server without the need for Google account synchronization. One chal ...

When utilizing JSON data in node.js, the .find() method may return undefined

I am currently working on a node server and my goal is to display JSON data when a specific ID is clicked. I have configured a dynamic URL that will retrieve the data of the clicked video using parameters and then compare it with the data in the JSON file ...

"ng-model not functioning properly with transcluded input"

I have created a simple directive that wraps an input element in a div using transclusion. However, I am facing an issue where it is breaking ng-model for some reason. You can view the full code on this plunker: http://plnkr.co/edit/tYTsVUbleZV1iG6eMgjo A ...

Error in Express Post Request: Headers cannot be modified after being sent to the client

I am a beginner in Node.js and I am facing some challenges while working on an app for learning purposes. I encountered the following issue: Error: Can't render headers after they are sent to the client. I am unsure of how to resolve it. C:\Us ...

By default, use jQuery to load the content of a div

Upon page load, I am looking to display the content within #destiny2. This section includes text and images, as well as additional content for three categories. Initially, the first category should be shown without requiring a click. Subsequently, clicking ...

What is causing the large build size when using Webpack with Vue.js?

After cloning the repository at: https://github.com/Microsoft/TypeScript-Vue-Starter I executed npm scripts: npm install npm run build The outcome: the size of build.js file is approximately 1MB. Can anyone explain why the build.js file is significant ...

Issue with Bootstrap 4.2 modal: Unable to edit input fields, button cannot be clicked, modal does not close

https://i.sstatic.net/vHnVG.png https://i.sstatic.net/MhU3f.png Utilizing Bootstrap 4.2.1, I have a modal that opens via a specific link: <a href="#" data-toggle="modal" data-target="#inviteByEmailPopup" data-backdrop="false" data-keyboard="false"> ...

Tips for using JQuery to update an HTML window from a separate window

After creating a new window with the code var w = window.open("", "_blank");, it initially displays the URL about: blank and allows me to easily write to its body. However, when examining the sources, I notice that it only contains `` with no content to ma ...

Send information from a form to a PHP script using axios

I'm having trouble correctly utilizing axios to send form data to a PHP script. Can anyone confirm if the syntax of my axios code is accurate? Additionally, I would like to know how I can verify the data that is being transmitted via the POST method. ...

How can you identify duplicate entries using Mongoose?

I am currently working on a create function and encountering some code that closely resembles this: if(req.body.test !== undefined) { if(--req.body.test EXISTS IN test (model)--) { DO STUFF } else { ...

Is there a way for me to pinpoint the location of an error in React?

Currently, I am operating a basic React application with webpack in development mode by using the following command: webpack -w --mode development --config ./webpack.config.js This setup ensures that my code remains unminified. However, I am encounterin ...

Using Angular with OpenStreetMap and $routeProvider allows for dynamic routing and

Check out this awesome single page app that I found: https://github.com/tombatossals/angular-leaflet-directive/blob/master/examples/simple-example.html However, I am looking to enhance it by adding a menu... <html ng-app="App"> <head> <lin ...

Dynamic item addition feature activated by button on contact form

I am looking to create a form that includes the standard name, phone, email fields as well as a dropdown for selecting products and a text box for inputting quantity. The unique aspect is allowing users to add multiple products (dropdown and quantity textb ...

Guide on redirecting to another webpage after submitting a Django form and securely saving form data to the backend database

Objective: Ensure that data submitted through a Django form is successfully saved on the admin side. Issue: Although able to redirect to another page after submitting the form, the data does not get saved on the admin side. How can I resolve this using ei ...

The local time displayed by Moment JS is not accurate and matches the time stored in the database

Is there a solution to displaying the createdAt field date based on the local timezone? I've attempted to implement it, but it only seems to work for localhost. When I upload it to the server, it displays the createdAt field data in UTC format. Below ...

Understanding the contrast between a put request subscription with an arrow versus without in Typescript

I'm sorry if the title is not very clear. In my Angular project, I have come across two different put requests: public Save() { const types: string[] = this.getTypes.getCurrentTypes(); this.userTypeService .updateTypes(this.userID, gro ...

What is the best way to receive input from a user, send it to a function as a string, and display the function's output on the screen?

I successfully added an input field to my webpage, but now I'm stuck on how to retrieve that input, pass it into a function as a string, and display the result on my page. Below you'll find my code, along with the function where I need the user& ...

Multipart form data processing without specifying files

I need help with uploading an image using node.js, express, and multiparty. My code is as follows: HTML <!DOCTYPE html> <html> <body> <form method="post" action="/img"> Select image to upload: <input type="file" name= ...

The functionality of 'rich-voice-editor' is currently not operational when trying to utilize the npm package known as quill-rich-voice-editor

Currently, I am utilizing an npm package within my Angular project. "quill": "^1.3.6", "quill-rich-voice-editor": "^0.4.0", Upon adding the line 'rich-voice-editor': true, it triggers the error message cor ...

Having difficulty persisting login session in Vue

When logging in via Google on my login page, I am able to retrieve the user parameters. However, when I refresh the page, the parameters are not saved. Here is the code snippet: export default { name: "App", data() { return { isLog ...