gpt4 book ai didi

javascript - 我如何从 mongodb 解析这个 json 以便与 Meteor Helper 一起使用

转载 作者:行者123 更新时间:2023-12-03 09:38:13 25 4
gpt4 key购买 nike

我在一个集合中有这个json数据,我需要获取first_yr、second_yr_sem_1、second_yr_sem_2 ....的所有字段/值。

  {
"first_yr": [
{
"subject_name": "Subject1",
"subject_val": "subject1"
},
{
"subject_name": "Subject2",
"subject_val": "subject2"
},
{
"subject_name": "Subject3",
"subject_val": "subject3"
},
{
"subject_name": "Subject4",
"subject_val": "subject4"
},
{
"subject_name": "Subject5",
"subject_val": "subject5"
},
{
"subject_name": "Subject6",
"subject_val": "subject6"
}
]

},
{
"second_yr_sem_1":
[
{
"subject_name": "Subject1",
"subject_val": "subject1"
},
{
"subject_name": "Subject2",
"subject_val": "subject2"
},
{
"subject_name": "Subject3",
"subject_val": "subject3"
},
{
"subject_name": "Subject4",
"subject_val": "subject4"
},
{
"subject_name": "Subject5",
"subject_val": "subject5"
},
{
"subject_name": "Subject6",
"subject_val": "subject6"
}
]

},
{
"second_yr_sem_2":
[
{
"subject_name": "Subject1",
"subject_val": "subject1"
},
{
"subject_name": "Subject2",
"subject_val": "subject2"
},
{
"subject_name": "Subject3",
"subject_val": "subject3"
},
{
"subject_name": "Subject4",
"subject_val": "subject4"
},
{
"subject_name": "Subject5",
"subject_val": "subject5"
},
{
"subject_name": "Subject6",
"subject_val": "subject6"
}
]

}

与 meteor 助手一起使用

<select name="subject">
{{#each subjects}}
<option value="{{subject_val}}">{{subject_name}}</option>
{{/each}}
</select>

那么,我应该返回给帮助者(主题)的 mongodb 查询应该是什么?!我被困住了,任何事情都可以帮助我!谢谢!!

最佳答案

抱歉,我不明白你的意思。因此,我假设您将 Json 直接保存在集合中,并且我认为您可以使用 cursor.forEach 将每个字符串解析为 template.helpers:

Template["myTemplate"].helpers({
var subjects = [];
var myDataCursor = MyCollection.find();
myDataCursor.forEach(function(eachSubject) {
subjects.push(JSON.parse(eachSubject)); // parse str to object, and save it in an array.
});
return subjects;
})

您可以找到更多详细信息here

我希望这能完美解决您的问题:-)

关于javascript - 我如何从 mongodb 解析这个 json 以便与 Meteor Helper 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31261626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com