我是一个新手,但是试图找出为什么我的GET请求返回一个空数组,即使我知道Mongo数据库集合不是空的。 WordForm集合中的每个单词形式都有一个“lexicalform”键,其值是对该集合中LexiconEntry对象的引用。当我使用LexiconEntry ObjectId作为参数提交GET请求时,它返回一个空数组而不是数组内容。这是我的文件:
我的控制器中的GET路线:
api.get('/wordforms/:id', (req, res) => {
WordForm.find({lexiconentry: req.params.id}, (err, wordforms) => {
if (err) {
res.send(err);
}
res.json(wordforms);
});
});
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);
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);
答案 0 :(得分:0)
根据上面给出的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);
});
});
lexicalform
架构中名为WordForm
的属性类似于您正在尝试的属性。因此,您可以使用此更改您的代码。
api.get('/wordforms/:id', (req, res) => {
WordForm.find({lexicalform: req.params.id}, (err, wordforms) => {
if (err) {
res.send(err);
}
res.json(wordforms);
});
});