2017-02-08 42 views
0

我正在爲考試練習構建移動應用程序。我需要一個API端點來提供考試細節和問題。我需要幫助在sails.js中構建我的數據。我有這個來自Firebase的JSON結構。sailsjs考試應用程序模型結構API

端點GET操作應該返回此:

  { 
      "8h9iuhiuhi89h98h": { 
       "exam": "waec", 
       "year": "1990", 
       "subject": "chemistry", 
       "question": "Which of these is not an acid", 
       "answers": [ 
       { 
       option: "NaCl", 
       isValid: true 
       }, 
       { 
       option: "H<sub>2</sub>SO<sub>4</sub>", 
       isValid: false 
       }, 
       { 
       option: "H<sub>3</sub>", 
       isValid: false 
       }, 
       { 
       option: "HCl", 
       isValid: false 
       } 
       ] 
      }, 
      "8h9iuhiuhi89h98h": { 
       "exam": "waec", 
       "year": "1990", 
       "subject": "chemistry", 
       "question": "Which of these is not an acid", 
       "answers": [ 
       { 
       option: "NaCl", 
       isValid: true 
       }, 
       { 
       option: "H<sub>2</sub>SO<sub>4</sub>", 
       isValid: false 
       }, 
       { 
       option: "H<sub>3</sub>", 
       isValid: false 
       }, 
       { 
       option: "HCl", 
       isValid: false 
       } 
       ] 
      }, 
"8h9iuhiuhi89h98h": { 
       "exam": "waec", 
       "year": "1990", 
       "subject": "chemistry", 
       "question": "Which of these is not an acid", 
       "answers": [ 
       { 
       option: "NaCl", 
       isValid: true 
       }, 
       { 
       option: "H<sub>2</sub>SO<sub>4</sub>", 
       isValid: false 
       }, 
       { 
       option: "H<sub>3</sub>", 
       isValid: false 
       }, 
       { 
       option: "HCl", 
       isValid: false 
       } 
       ] 
      }, 
      "8h9iuhiuhi89h98h": { 
       "exam": "waec", 
       "year": "1990", 
       "subject": "chemistry", 
       "question": "Which of these is not an acid", 
       "answers": [ 
       { 
       option: "NaCl", 
       isValid: true 
       }, 
       { 
       option: "H<sub>2</sub>SO<sub>4</sub>", 
       isValid: false 
       }, 
       { 
       option: "H<sub>3</sub>", 
       isValid: false 
       }, 
       { 
       option: "HCl", 
       isValid: false 
       } 
       ] 
      }  
    } 

其中WAEC是考試,是1990年的一年,和化學是主體。

http://someappurl.com/api/exam/{exam}/{year}/{subject} 

我已經使用sails generate命令生成了API。但我不知道如何構建和查詢我的數據。我如何構建我的模式? 我有這樣的風帆模型結構

attributes: { 
    exams: { 
     exam_name: 'string', 
     years: { 
     exam_year: 'string', 
     subjects:[ 
      { 
      subject_name: 'string', 
      questions: [ 
       { 
       serial_no: 'string', 
       text: 'string', 
       answers:[ 
        { 
        option: 'string', 
        is_valid: 'boolean' 
        } 
       ] 
       } 
      ] 
      } 
     ] 
     } 
    } 
    } 
+0

你的JSON爲n無效。你有重複的鍵和大量的元素名稱不包含在雙引號。請更正此問題,然後通過格式化程序運行它,否則人們應該如何閱讀它? –

回答

0

您沒有正確使用該模型。認爲每個Model.js是一個表,每個屬性是一個列。

所以,你可以Exam.js是這樣的:

// api/models/Exam.js 
module.exports = { 
    attributes: { 
    name: { 
     type: 'string', 
    }, 
    year: { 
     type: 'integer' 
    }, 
    subject: { 
     type: 'string', 
    }, 
    question: { 
     type: 'string', 
    }, 
    answers: { 
     type: 'array', 
    } 
    } 
}; 

和控制器上,您將定義如何從數據庫中顯示的數據。 您可以瞭解更多關於帆船模型和控制器上的文檔:

示例項目: