简体   繁体   English

GET请求返回空数组而不是数组内容

[英]GET request returns empty array instead of array contents

I'm a newbie, but trying to figure out why my GET request returns an empty array even though I know that the Mongo database collection isn't empty. 我是一个新手,但试图弄清楚为什么我的GET请求返回一个空数组,即使我知道Mongo数据库集合不是空的。 Each word form in the WordForm collection has a "lexicalform" key whose value is a reference to a LexiconEntry object in that collection. WordForm集合中的每个单词形式都有一个“lexicalform”键,其值是对该集合中LexiconEntry对象的引用。 When I submit a GET request with a LexiconEntry ObjectId as a parameter, it returns an empty array in stead of the array contents. 当我使用LexiconEntry ObjectId作为参数提交GET请求时,它返回一个空数组而不是数组内容。 Here are my files: 这是我的文件:

The GET route in my controller: 我控制器中的GET路由:

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: LexiconEntry模型:

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: WordForms模型:

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);

Based on your WordForm schema given above, there is no such property lexiconentry exists in WordForm schema as in your query below. 根据您的WordForm上面给出的架构,有没有这样的属性lexiconentry中存在WordForm架构如下查询。

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

The property called lexicalform in WordForm schema resembled the one you are trying above. WordForm模式中称为lexicalform的属性类似于您在上面尝试的属性。 So, you can change your code as below with this. 因此,您可以使用此更改您的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM