Ich bin relativ neu zu Mungo und habe versucht, nach einer Antwort suchen, aber nichts scheint bisher zu funktionieren.Mungo Antwort nicht richtig analysieren
Ich frage meine mongoDB (gehostet auf mlab) und möchte nur ein Objektliteral an die Front-End-Vorlage (mit Hoganjs für Templates neben Express für Routing) übergeben.
Wenn ich es zwar übergebe, ist die Antwort kein Objektliteral und auch wenn ich JSON.parse auf die Antwort mache, funktioniert es immer noch nicht.
Ich brauche es ein Array mit Objektliteralen der Elemente in der Sammlung gefüllt, so dass auf der Vorderseite kann ich die zurückgegebenen Elemente durchlaufen.
Wie kann ich das so funktionieren? Ich schaute auf die Dokumente und einige Stapelüberlaufpfosten, aber wie gesagt, nichts funktioniert so.
Hier ist meine aktuellen Code:
index.js (Strecke):
DB-Verbindung:
mongoose.connect("CONNECTION URL IS HERE, JUST REMOVED SINCE I AM POSTING THIS SNIPPET ONLINE");
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
var timelineItemsSchema = mongoose.Schema({
postedTime: String,
postedPlace: String,
postedContent: String,
postedTitle: String,
});
var timelineItems = mongoose.model('timelime_items', timelineItemsSchema);
timelineItem = new timelineItems({
"postedTime": "21/03/2016 13:44",
"postedPlace": "facebook",
"postedContent": "blah blah blah?",
"postedTitle": "Post Two"
});
timelineItem.save(function(err, item) {
if (err) return console.error(err);
console.log("item saved!")
});
timelineItems.find(function(err, items) {
if (err) return console.error(err);
dbResponseItems = items;
})
});
Vorbei an das vordere Ende:
res.render('index', {
page_title: "Timeline",
author: "",
nav_links: link_info,
dbResponse: dbResponseItems
});
Und das vordere Ende erwartet die folgenden ausgeführt werden:
(function() {
let $timeline = $("ul.timeline"),
jsonResponse = {{dbResponse}}; //Array of the object literals to be looped should go here
//and I should be able to loop it here, I know this loop works because I had a "dummy" array of object literals during prototyping, now I need an actual one to come back from the db filled with the object literals
for (let i = 0; i < jsonResponse.length; i++) {
let postedTime = jsonResponse[i].postedTime,
postedPlace = jsonResponse[i].postedPlace,
postedContent = jsonResponse[i].postedContent,
postedTitle = jsonResponse[i].postedTitle,
templateOne = `<li>
\t \t \t <div class="timeline-badge"><i class="glyphicon glyphicon-plus"></i></div>
\t \t \t <div class="timeline-panel">
\t \t \t \t <div class="timeline-heading">
\t \t \t \t \t <h4 class="timeline-title">${postedTitle}</h4>
\t \t \t \t \t <p><small class="text-muted"><i class="glyphicon glyphicon-time"></i> ${postedTime} via ${postedPlace}</small></p>
\t \t \t \t </div>
\t \t \t \t <div class="timeline-body">
\t \t \t \t \t <p>
\t \t ${postedContent} \t \t \t \t
\t </p>
\t \t \t \t </div>
\t \t \t </div>
\t \t </li>`,
templateTwo = `<li class="timeline-inverted">
\t \t \t <div class="timeline-badge"><i class="glyphicon glyphicon-minus"></i></div>
\t \t \t <div class="timeline-panel">
\t \t \t \t <div class="timeline-heading">
\t \t \t \t \t <h4 class="timeline-title">
\t \t ${postedTitle}
</h4>
<p><small class="text-muted"><i class="glyphicon glyphicon-time"></i> ${postedTime} via ${postedPlace}</small></p>
\t \t \t \t </div>
\t \t \t \t <div class="timeline-body">
\t \t \t \t \t <p>
\t \t ${postedContent} \t \t \t \t
\t </p>
\t \t \t \t </div>
\t \t \t </div>
\t \t </li>`;
if (i % 2 === 0) { // index is even
$timeline.append(templateOne);
} else { //index is odd
$timeline.append(templateTwo);
}
}
})();
Irgendwelche Ideen?
Wenn Sie weitere Informationen benötigen, fragen Sie einfach im Kommentarbereich, ohne Zweifel ist es wahrscheinlich etwas, was ich falsch verstanden habe, aber ich bin nach 30 - 40 Minuten des Suchens nach einer Lösung verloren!
Auch die zurückgegebenen Elemente in der ungeprüften, seltsame Antwort sind alle URI-codiert, wie können Sie das stoppen? – SkullDev