Error message: Unable to locate property 'indexOf' in an undefined object

Encountering an error message that says, "Cannot read property 'indexOf' of undefined". Despite receiving the journey passport ID in the view, it seems like the journey passport ID is not being passed to the function. Additionally, console.logs are not showing up for some unknown reason. Any assistance on this issue would be highly appreciated!

Shown below is my HTML code calling the 'userLikes' filter:
(Full code available at: https://github.com/cdtdesign/TCP-Express/blob/master/views/blog.html)

<a class="like-button" title="Like">
                  <i class="fa fa-heart {{journey.passport_id | userLikes}}"></i>
                </a>
                {% if user.passport_id == journey.passport_id %}
                <a href="#modal-text/{{ String(journey._id) }}#update" title="Edit">
                  <i class="fa fa-pencil journeyEditButton"></i>
                </a>
                <a title="Delete">
                  <i class="fa fa-close journeyDeleteButton" style="font-size:2.875rem;" data-journey-id="{{ String(journey._id) }}"></i>
                </a>
                {% endif %}

The problematic JavaScript section is as follows:
(Full code link: https://github.com/cdtdesign/TCP-Express/blob/master/routes/blog.js)

if (req.user) {
    swig.setFilter('userLikes', function(journeyPassportID) {
      if (req.user.journeys_liked.indexOf(journeyPassportID) != -1) {
        return 'liked';
      }
    });

In addition, here's the relevant code snippet from my Journey model where the passport_id is defined:

var journeySchema = new Schema({
    uuid: {
        type: String,
        default: uuid.v4()
    },
  travel_token: {
        type: String,
        default: randomstring.generate()
    },
  passport_id: String,
  traveler_name: String,
  title: String,
  date: Date,
  body: String,
  description_filename: String,
  header_image_filename: String,
  users_who_like: Array,
  tags: String,
    shortlink: String,
  deleted_at: String,
  created_at: {
        type: Date,
        default: Date.now
    },
  updated_at: {
        type: Date,
        default: Date.now
    }
});

Answer №1

journeys_liked is not being saved in the req.user object. If it's returning as undefined, then there may be no value present for the .indexOf() method to operate on, specifically with journeys_liked. It would be advisable to confirm whether there should indeed be a value for that property in the database. If it is expected to have a value, the issue might lie with Passport failing to store this value within req.user.

In case of absence of any value in the database, adding a conditional check for its presence could be more practical. This way, the code will only execute if there exists a value like

if (req.user.journeys_liked && req.user.journeys_liked.indexOf()
.

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

Executing functions in real-time using React Native

I'm fairly new to Object-Oriented Programming (OOP) and my understanding of promises, asynchronous/synchronous function execution is quite basic. Any guidance or help from your end would be greatly appreciated! Let's take a look at an example fr ...

Clear the contents of a textarea once user has finished inputting

I am currently working with AngularJS to develop a basic Single Page Application (SPA). Within this application, there is a <textarea> element that displays initial content from a binding: <textarea>{{activeField.rawContent}}</textarea> ...

Angular is giving me an undefined Array, even though I clearly defined it beforehand

I've been working on integrating the Google Books API into my project. Initially, I set up the interfaces successfully and then created a method that would return an Array with a list of Books. public getBooks(): Observable<Book[]>{ ...

`amqplib - What is the current number of active consumers on the queue?`

Seeking insight on using the amqplib module for Node JS and RabbitMQ: 1) Is there a method to determine the number of subscribers on a queue? 2) What is the process for ensuring that each queue has only one consumer key? Appreciate any guidance! ...

What is the proper method for securing this?

Trying to retrieve 'this' within a method that is invoked by pressing a button, where this points to both the class and the pressed button: p.myVar = 'apple'; $('.go').on('click', this._init); p._init = function(e ...

AngularJS - Introducing blurred delay

Here is the code snippet I am working with: <div class="staff"> <input ng-model="user" ng-focus="isFocused = true;" ng-blur="isFocused = false;/> <div class="matches" ng-if="isFocused"> <div ...

Retrieve information from MariaDB using nodejs and transmit it to an html document (Express JS)

To create a calendar with activities, I am retrieving data from a local Maria database and populating an HTML table. My approach involves using Express JS to direct users to a designated HTML page. Here is how my file structure looks like: NODE | |- app ...

Using Vue.js to share events and data across various components

I am currently working on an application that features a Google map with a places autocomplete controller, similar to the example provided by Google at this link. Whenever an address is searched or selected, or when the map bounds are changed, I trigger a ...

Filtering out section boxes does not eliminate empty spaces

Link to Fiddle I've run into a bit of a roadblock while trying to filter my section box for a project I'm currently working on. The issue I'm facing is that instead of collapsing the first section box to display only the filtered options, i ...

Can test files be placed under the pages directory in a Next.js project?

Currently, I am in the process of writing integration tests for the API routes within a Next.js application. One question that has arisen is whether it would be problematic to place the index.test.ts file under the /pages directory. My preference leans tow ...

Ways to activate a disabled button based on a condition in vue.js?

I am currently facing an issue with the 'submit' button that I have disabled under van-submit-bar. My requirement is to enable it once the user chooses a table number from the dropdown options. The default selection in the drop-down menu is &a ...

Name of Document (changing from php to ?)

Greetings! Currently, I am utilizing PHP to include the document name as an example. folder/<?phpecho basename(__FILE__, '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));?>_button.png I have observed that using PHP for this purpose and havin ...

Is there a way to programmatically emulate Firebug's inspect element feature using JavaScript?

Is there a straightforward method in JavaScript or any popular libraries like YUI or jQuery to allow users to interact with elements on a website, similar to the functionality of Firebug for developers? ...

Display or conceal content depending on the height of a div element

I need help with the following code. I want to hide the second paragraph by default and show it only when the blue circle is clicked and the div is expanded. Here is the code: What this code does is calculate the inner height of a specific element and s ...

What is the method for adding two rows of a 2D array element by element in C and treating it as a sum of vectors?

Is there a simple way to sum two rows of a 2D array in C without using a loop? In the main function, I have the following sample code: for (i = 0; i < 3; i++) { (*A)[i] = drand48(); (*B)[i] = drand48(); } I am working with two pointers (A and ...

Implementing the Context API with React Router: using version '^5.2.0' of react-router-dom and version '17.0.2' of React

I am currently struggling to retrieve the user value from my Header component using the Context API, but unfortunately it is returning as undefined. Below is my code snippet. import React, { createContext, useEffect, useState } from "react"; exp ...

Dealing with ASP.NET forms that involve a javascript plugin generating substitute input

I'm facing an issue with my ASP.NET MVC webpage where I submit a form using AJAX in the following way: function ValidateFormAndAjaxSubmit(formId, callingElement) { if (IsNotDblClick(callingElement.id)) { var _form = $("#" + formId); ...

Upgrading an Express 2.x application to Express 3.0

I am currently studying NodeJs and Express and am in the process of converting a tutorial app from Express 2.5.9 to version 3.0. The following code is now causing an error "500 Error: Failed to lookup view "views/login". How can I update this to render cor ...

Is utilizing while(*p++) a risky practice?

Can using while(*p++) to check for additional elements in an array lead to potential issues? What happens if the next memory location contains a value that is not part of the array? This straightforward code snippet: #include <stdio.h> void f(flo ...

Creating objects in Angular 2 through HTTP GET calls

Recently, I've delved into learning Angular 2. My current challenge involves making http get requests to retrieve data and then constructing objects from that data for later display using templates. If you believe my approach is incorrect, please feel ...