The GET request is returning a null array instead of the expected array contents

I am new to this, but I am trying to understand why my GET request is returning an empty array even though I am sure that the Mongo database collection is not empty. Each word form in the WordForm collection has a "lexicalform" key that refers to a LexiconEntry object in that collection. When I make a GET request with a LexiconEntry ObjectId as a parameter, it returns an empty array instead of the actual array contents. Below are the relevant files:

The GET route in my controller:

api.get('/wordforms/:id', (req, res) => {
    WordForm.find({lexiconentry: req.params.id}, (err, wordforms) => {
      if (err) {
        res.send(err);
      }
      res.json(wordforms);
    });
  });

The LexiconEntry model:

import mongoose from 'mongoose';
import WordForm from './wordform';
let Schema = mongoose.Schema;

let LexiconEntrySchema = new Schema({
  lexicalform: String,
  pos: String,
  gender: String,
  genderfull: String,
  decl: String,
  gloss: [String],
  meaning: String,
  pparts: [String],
  tags: [String],
  occurrences: Number,
  wordforms: [{type: Schema.Types.ObjectId, ref: 'Form'}]
});

module.exports = mongoose.model('LexiconEntry', LexiconEntrySchema);

The WordForms model:

import mongoose from 'mongoose';
import LexiconEntry from './lexiconentry';
let Schema = mongoose.Schema;

let WordFormSchema = new Schema({
  form: String,
  gender: String,
  case: String,
  number: String,
  lexicalform: {
    type: Schema.Types.ObjectId,
    ref: 'LexicalForm',
    required: true
  }
});

module.exports = mongoose.model('WordForm', WordFormSchema);

Answer №1

After reviewing your WordForm schema provided, it appears that there is no property named lexiconentry within the WordForm schema as mentioned in your query.

api.get('/wordforms/:id', (req, res) => {
    WordForm.find({lexiconentry: req.params.id}, (err, wordforms) => {
      if (err) {
        res.send(err);
      }
      res.json(wordforms);
    });
  });

The correct property to use in the WordForm schema is actually called lexicalform, which aligns with what you are attempting to do in your query. Therefore, you should adjust your code as follows:

api.get('/wordforms/:id', (req, res) => {
    WordForm.find({lexicalform: req.params.id}, (err, wordforms) => {
      if (err) {
        res.send(err);
      }
      res.json(wordforms);
    });
  });

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 are the compatibility considerations for npm packages with Angular 2? How can I determine which packages will be supported?

When working with Angular 2, do NPM packages need to be modified for compatibility or can any existing package work seamlessly? If there are compatibility issues, how can one determine which packages will work? For instance, let's consider importing ...

Select a date from the JQuery calendar, verify it against the database, and display any events scheduled for that date

I am working with a jQuery calendar that stores the selected date in a variable named "X". My goal is to retrieve events stored on that specific date from the Database and display them. Can anyone provide some guidance? <div id="calendar"></div&g ...

An elegant approach to converting a JavaScript object containing key-value pairs into an array of objects, each with a single key-value pair

Essentially, I have an enum that represents different statuses status = {1: "new", 2: "working" ... } and my goal is to transform it into something like status = [{1: "new"}, {2: "working"} ...] in a way that is cl ...

Angular JS appears to be causing the DOM to freeze up while utilizing the ng-repeat directive to loop through

I have a current app where clicking a button triggers an $http request to fetch and return some data. The retrieved information is then used to update the $scope variables rows and columns, which are then looped through using ng-repeat. However, I've ...

The socket.on() function is not able to receive any data

I am encountering an issue with implementing socket.on functionality $('#showmsg').click(function() { var socket = io.connect('http://localhost:3000'); var msgText = $('#msgtext'); socket.emit('show msg', msgText.va ...

Enhance the visual appeal of Autodesk Forge Viewer by incorporating environmental textures

Is there a way to incorporate an environmental texture into Autodesk Forge Viewer v6.0? I know in Three.js you can apply a texture to the scene's background and environment, so how would I achieve this in APS viewer? I'm not looking for a skybox ...

Handlers for event(s) not triggering on video element

In my NextJS application, I have implemented a feature to display a video file using the video element. I have added multiple event handlers to the video element: <video className='video-player' controls={true} preload='metadata&apo ...

What is the process for displaying all indexes of a Mongo Collection using MongoDB Node native?

I am curious about how to retrieve all indexes from a specific collection in mongodb. I've tried using listIndexes, indexes, and indexInformation, but these methods only return empty values (arrays and objects). However, when I run db.getCollection(&a ...

Leveraging npm in vanilla JavaScript applications

Because of limitations set by the governance of my current project, I am unable to utilize many of the modern JS libraries and frameworks. Therefore, for our MVP, we are resorting to using vanilla JS directly loaded to the client (un-minified), which is no ...

Issue with fulfilling Ajax promises

How can I properly use a promise to compare the current logged-in user with a field from a list in SharePoint? function compareCurrentUserWithListObject() { var userProp = this._userProfileProperties; var userName = userProp.get_userProfilePropertie ...

Can a 1D array be utilized to create a 2D grid in React?

I needed to display a one-dimensional array in a 2D grid format with 3 rows and 3 columns. The array itself contains numbers from 1 to 9. const array = Array.from({ length: 9 }, (_, i) => i + 1); In my React component, I have it rendering as a series o ...

What is the best method for organizing data in rows and columns?

I attempted to use my map function to iterate over the data and display it, but I struggled to format it into rows and columns. The requirement is for 5 fixed columns with dynamically changing rows, making array indexing impractical. Here is the code snip ...

Utilizing JSON format for processing HTTP requests in JavaScript with Node.js

I'm working with a snippet that retrieves data in JSON format, but I'm interested in manipulating the data instead of just outputting it to the console. var request = require('request'); var headers = { 'Connection': ' ...

Issue with data-ng-class function not being invoked

I'm currently working on a simple Angular project where I need to dynamically color list items based on a function called in data-ng-class. Below is an excerpt from my HTML file: <div> Rooms:<ul style="list-style:none;"> < ...

Exploring the benefits of incorporating layout view in Express using Consolidate and Mustache

Recently, I delved into using Node with Express and successfully integrated Consolidate JS to implement Mustache as the templating view system by following the guidelines on the Consolidate JS Github page. Although Mustache is loading correctly, I am now ...

Steps for combining two collections into a single output in MongoDB with Node.js

My MongoDB collections consist of data that I need to merge using the $lookup operation. However, the result I get contains a nested array structure that is not ideal. I am looking for a solution to format the result as shown below: First collection locati ...

Shifting the position of an HTML page to one direction

I'm currently working on adding a sidebar to my Twitter Bootstrap 3 project. The goal is to have a fixed positioned nav nav-pills nav-stacked show up on the left side of the page when a button is clicked. I've set its z-index to 1000 so it appear ...

Neglecting the error message for type assignment in the Typescript compiler

Presented here is a scenario I am facing: const customer = new Customer(); let customerViewModel = new CustomerLayoutViewModel(); customerViewModel = customer; Despite both Customer and CustomerLayoutViewModel being identical at the moment, there is no ...

Executing a prop function within the useEffect hook: a step-by-step guide

I am attempting to address this warning in a react component Line 19:8: React Hook useEffect has a missing dependency: 'handleChange'. Either include it or remove the dependency array react-hooks/exhaustive-deps This is the component: ...

Refreshing the Mean stack front-end

I'm experiencing difficulties with updating the client side CRUD logic. The current setup is causing the fields to be deleted. What could I be missing? Here is my Angular code: $scope.editService = function(id) { $http.put('/api/hc/&apo ...