2016-11-26 141 views
0

我是一个新手,但试图弄清楚为什么我的GET请求返回一个空数组,即使我知道Mongo数据库集合不是空的。 WordForm集合中的每个单词形式都有一个“词法形式”键,其值是对该集合中的LexiconEntry对象的引用。当我使用LexiconEntry ObjectId作为参数提交GET请求时,它将返回一个空数组而不是数组内容。下面是我的文件: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); 

的词形等模型:基于您的WordForm模式

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

你可以试试下面的建议答案吗?您已将模型名称作为查询中的属性。 – Aruna

回答

0

如上所述,在012中不存在这样的属性架构,如下面的查询。

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

酒店叫lexicalformWordForm模式类似于你在上面尝试之一。所以,你可以用下面的代码改变你的代码。

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

工作正常!非常感谢!! –