2016-11-26 146 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

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