2014-03-19 83 views
0
數據未能

我正在用流星測試/測驗的應用程序,並擁有所有的問題,可能的答案等存儲在本地的MongoDB像這樣:(流星)#each從MongoDB的

{ 
type: "someType", 
skillType: "someSkillType", 
questions: [ 
    { 
    questionID: 1, 
    question: "Some question", 
    answer: 2, 
    option1: "Some possible answer", 
    option2: "Another one", 
    option3: "Etc." 
    }, 
    { 
    questionID: 2, 
    question: "Some question 2", 
    answer: 1, 
    option1: "Some possible answer", 
    option2: "Another one", 
    option3: "Etc." 
    } 
    ] 
} 

等等。我已經測試並確保它不是我的Meteor.CollectionMeteor.publish/Meteor.subscribe或與數據庫的連接問題。數據在那裏,我可以從控制檯訪問它很好。

我的模板看起來是這樣的:

<template name="test"> 
    <form name="testForm" id="testForm" role="form"> 
     {{#each testQuestions}} 
      <div class="jumbotron"> 
       <p><b>{{questionID}}.</b> {{question}}</p> 
        <div class="radio"> 
         <label> 
          <input type="radio" name="qNumber{{questionID}}" value="1" required> 
           {{option1}} 
          </label> 
        </div> 
        <div class="radio"> 
         <label> 
          <input type="radio" name="qNumber{{questionID}}" value="2"> 
           {{option2}} 
         </label> 
        </div> 
        <div class="radio"> 
         <label> 
          <input type="radio" name="qNumber{{questionID}}" value="3"> 
           {{option3}} 
         </label> 
        </div> 
      </div> 
     {{/each}} 
    </form> 
</template> 

我然後做一個模板幫手來獲取數據:

Template.test.testQuestions = function() { 
    questionsAll = allQuestions.findOne({"type": "someType", "skillType": "someSkillType"}, {fields: {"_id": 0, "type": 0, "skillType": 0, "questions.answer": 0 }}); 
    questionsAll1 = EJSON.toJSONValue((questionsAll.questions)); // I've tried both with and without this part. 
    return questionsAll1; 
} 

但是,它沒有工作,沒有被渲染和我在控制檯中得到一個非常長的錯誤,開始於:

"Exception from Deps recompute function: [email protected] 

如果我讓一個sta tic數組直接在模板助手中,但它工作正常。我覺得我已經試過了所有的東西,而我根本找不到原因。任何幫助或想法?

回答

0

這看起來像是一種需要添加警衛的情況,因爲findOne在預訂準備就緒之前可能會返回undefined。你可以這樣做:

Template.test.testQuestions = function() { 
    var aq = allQuestions.findOne({ 
    type: 'someType', 
    skillType: 'someSkillType' 
    }); 

    if (aq && aq.questions) { 
    return aq.questions; 
    } 
}; 

你不需要任何轉換與EJSON.toJSONValue。另請注意,qid似乎不存在於您的示例數據中。也許你的意思是questionID

+0

是的,我的意思是'questionID'而不是'qid' - 感謝您指出:)這是數據示例中的錯誤。 我剛剛嘗試了你的建議,並沒有骰子不幸。我仍然得到相同(巨大)的錯誤。 – Runeks

+0

給最新的答案一個嘗試 - 它爲我工作。 –

+0

這是......很棒。有用!並不是說我認爲if語句應該有所作爲,但顯然它確實有意義。如果可以的話,我會給你在這個世界上的所有聲望 - 我一直在爲這個瘋狂地撓撓我的頭。再次感謝! – Runeks