Issue encountered: TypeError - Attempting to access an undefined object in drug search services when trying to retrieve drugs based on specified criteria

MedicationSearchService.js

import axios from "axios";
    const MEDICATION_GENERIC_API_URL ='http://localhost:8080/medication/search/generic/'
    const MEDICATION_MAIN_API_URL ='http://localhost:8080/medication/search/main/'

    class MedicationSearchService{
        getMedicationsByCriteria(criteria,val){
            if(criteria == 'Main'){
                return axios.get(MEDICATION_MAIN_API_URL+val);
            }else if(criteria == 'Generic'){
                return axios.get(MEDICATION_GENERIC_API_URL+val);
            }
        
        }
    }

export default new MedicationSearchService()

MedicationSearch.vue

    <template>
    <div class="input-group">
            <input v-model="val" type="text">
            <select v-model="criteria">
                <option value="Main" selected>Main Name</option>
                <option value="Generic">Generic Name</option>
            </select>
            <button type="submit" v-on:click="get_Medications_by_criteria()">Search</button>
    </div>
    </template>

    <script>
    import medicationSearchService from '@/services/medicationSearchService';
    export default{
        name:'Medications_by_criteria',
        data(){
            return{
                criteria:'',
                val : '',
                medicationsByCriteria:[]
            }
        },
        methods:{
            get_Medications_by_criteria(){
                // medicationSearchService.getMedicationsByCriteria(this.criteria,this.val).then((response)=>{
                medicationSearchService.getMedicationsByCriteria('Main','Loratadine').then((response)=>{
                    this.medicationsByCriteria = response.data;
                });
            }
        },
        created(){
            this.get_Medications_by_criteria()
        }
    }

</script>

error:

Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating'services_medicationSearchService__WEBPACK_IMPORTED_MODULE_0_["default"].getMedicationsByCriteria(this.criteria, this.val).then')

want to use like this

medicationSearchService.getMedicationsByCriteria(this.criteria,this.val).then((response)=>{

but getting error. but same method with static data is working like this

medicationSearchService.getMedicationsByCriteria('Main','Loratadine').then((response)=>{

what's wrong in this

medicationSearchService.getMedicationsByCriteria(this.criteria,this.val).then((response)=>{

Answer №1

I set up the criteria and val variables with initial values, and everything seems to be functioning properly now.

data(){
    return{
        criteria:'Main',
        val : 'Metformin',
        drugsByCriteria:[]
    }
},

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

Enhancing User Experience with React Hover Effects

I'm currently in the process of developing a React component library, and I'm looking to enhance the components with some stylish flair. Everything seems to be working smoothly, but I'm interested in changing the background color when a user ...

NodeJS Express REST API fails to fetch data in GET request

As I work on developing a REST API using NodeJS and Express, I have set up routes for handling "offers". However, when attempting to send a GET request, I receive an empty response along with a 200 status code. Here is the snippet from routes.js: route ...

Converting a JSON object into a different format using TypeScript

Looking for advice on how to efficiently convert JSON data into a specific format without hardcoding any values like "root" or "Amount". I want to create a reusable function that can be used in various scenarios. Currently, I am working with TypeScript and ...

PDF viewing problem in iOS 11.3 when accessing PWA

I have successfully integrated Vuejs and Framework7 into my PWA. I am currently facing a challenge where I need to open a remote PDF file in my PWA while ensuring that users can easily navigate back to the PWA after viewing the PDF. The solution I initiall ...

Save OpenLayers map as a PNG file using a personalized file name

I am trying to save my map as a .png image but I am encountering some difficulties. The code I have been using is supposed to allow me to do so, but it seems like there might be something missing because I cannot save the image under a custom name. documen ...

Puppeteer causes Express to stop listening to incoming requests

As I work on developing a straightforward API that utilizes Puppeteer to execute actions, I encounter an issue where my Express app stops listening once Puppeteer is launched. Below is the script in question: const Apify = require('apify'); cons ...

Struggling with setting up the router

Presented below is the code snippet from my router/index.js file: import Vue from 'vue' import Router from 'vue-router' import Home from '../views/Home' import Login from '../views/Login' Vue.use(Router) export de ...

In Javascript, I can add an element at a specific index in an array by inserting elements from a different

I am faced with a coding problem involving two separate Javascript arrays: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12&apo ...

Transforming JSON data into XML using Angular 7

It turns out the xml2js npm package I was using doesn't support converting JSON to XML, which is exactly what I need for my API that communicates with an application only accepting XML format. In my service file shipment.service.ts import { Injecta ...

Prevent scrolling to the top when using router.push in Next.js

Currently, I am utilizing the router feature from Next.js by importing useRouter from next/router. I am currently facing an issue where the page automatically scrolls to the top when changing the query parameters in the URL. Is there a solution available ...

GWT's Stylish Image Carousel

Can anyone recommend a simple image slider for GWT that meets the following requirements: Slides several images from right to left when user clicks on the image (selected image should be at the center) Has endless looping capability (the last image shoul ...

Sequencing requests and processing data in Node.js through event handling

Is there a way to combine the responses from two requests into one single JSON response? The goal is to have an array containing both {response1JSON} and {response2JSON}, with each response streaming data that needs to be read. function getSongs() { c ...

Encountering a top-level-await issue while utilizing the NextJS API

Currently, I am in the process of creating an API using NextJS and MongoDB. To start off, I have set up some basic code at the beginning of the API file: const { db } = await connectToDatabase(); const scheduled = db.collection('scheduled'); Fol ...

Navigating to a different page in the app following a document update: a step-by-step guide

I am facing an issue with a page where I am trying to print a specific DIV using the script below... function printReceiptDiv() { var divElements; if (vm.isDLR) { divElements = document.getElementById("DLRreportCont ...

How can you extract the text associated with the chosen value in a dropdown list implemented in JavaScript?

Hello, I've been having difficulty extracting the value from a dropdown list created using the FacetWP plugin. Despite extensive research and numerous attempts using various methods to retrieve the text value from the list, I have not been successful. ...

Stop Swiper Slide from moving when clicked on

I am currently utilizing Swiper JS and have encountered an issue. In my Swiper slider, each slide contains a button. Whenever I click on the slide or the button itself, the slide becomes the active one, causing the entire slider to move. Is there a way to ...

Is it possible for Typescript to encounter an error when using console.log() with a variable of type "any"?

When using console.log(variable: any) in Typescript, I am concerned about potential errors and avoiding the need for try{}catch blocks throughout my code. Will console.log(any) trigger any errors or will it successfully print any input provided? public ...

C# Rest Client Tool that Works with Various REST API's

I am looking to develop a REST client in C# that can dynamically read API call details from a configuration file, process the JSON responses received, and generate corresponding CSV files. Additionally, there will be another configuration file specifying a ...

What is the best way to access a variable from one JavaScript file in a different file?

Currently, I have a simple cube scene set up in Three.js Now, my goal is to add camera movement using perlin noise. To achieve this, I found and incorporated the Perlin noise library into my project: https://github.com/josephg/noisejs I then included th ...

Fetching text box value that has already been assigned dynamically using JavaScript

I created a PHP form that looks like this: <?php $txtVal1 = "value1"; ?> <script type="text/javascript"> function post_data(){ alert(document.getElementById("text1").value); } </script> <form method="post" action=""> <in ...