【问题标题】:JSON-File - print multiple informationsJSON-File - 打印多个信息
【发布时间】:2019-01-05 08:32:32
【问题描述】:

我的 JSON 数据存在一些问题。我正在建立一个网站,用户应该能够在其中创建食谱。为此,他可以输入名称、成分和烹饪过程。在我的网站上,用户可以找到食谱并订阅它们。问题是,有多种成分,但在我的网站上,总是只有第一个项目......就像它没有读取整个数组一样。

我的 JSON 如下所示:

"_id" : ObjectId("5c2ff33debbe541cdc500594"),
    "subscriber" : [
            {
                    "_id" : ObjectId("5c2ff340ebbe541cdc500595"),
                    "userID" : "5c2ff31bfa14ec17b8ad10c4",
                    "username" : "Dominik"
            }
    ],
    "titel" : "Spaghetti Bolognese",
    "theme" : "Meat",
    "ingredients" : "Noodles, Meat, Sauce",
    "discription" : "Cook Noodles and meat. Cook the sauce."}

如您所见,系统正在正确保存信息。我的代码打印所有这些都是这样的

<div id="content">
<% if(subscriptions.length>0) { %>
    <% for(var i=0; i < subscriptions.length; i++) { %>
        <% if("") { %>
        <%} else {%>
        <div id="recipe_titel"><%=subscriptions[i].titel%></div>
        <div id="recipe_ingredients"><%=subscriptions[i].ingredients%></div>
        <div id="recipe_discription"><%=subscriptions[i].discription%></div>
            <td><button type="submit" class="button_unsubscribe" value="<%=subscriptions[i]._id%> <%=subscriptions[i].recipeID%>" id="button_small">Subscribe</button></td>
        </tr>
        <% } %>
    <% } %>
    <% } %>

在我的网站上结果是

Spaghetti
Bolognese
Noodles,

我不知道,为什么系统没有打印正确的结果……它被正确保存了。

【问题讨论】:

    标签: html css json


    【解决方案1】:

    首先不清楚,你甚至尝试在哪里使用subscriptions[i].ingredients

    不仅如此,"ingredients" : "Noodles, Meat, Sauce" 不是一个数组,也许你应该使用"ingredients" : ["Noodles", "Meat", "Sauce"]

    【讨论】:

    • 对不起,有一点打字错误。我纠正了它。
    • 好的,然后检查你的字符串,先尝试删除空格和逗号,结果如何?然后尝试加空格,打勾,然后逗号,打勾,然后第二个空格等。我想,可能有一些不可读的字母,例如换行符......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多