2016-09-12 4 views
0

Ich habe gerade einige Code geschrieben, die JSON-Daten in das DOM lädt. Dies ist der vollständige Code:Piping Variablen in res.Render Funktion Parameter Namen in express

var express = require('express'); 
var router = express.Router(); 

// var money = require('./routes/money'); 

var duckStorySection = { 
    para1: "I first met Boa-Duck through Lene-Cow's intervention. I had been scoping out the possibility of an illicit affair with The Green Party of England And Wales' Amelia Womack, but the party propaganda machine had other ideas. Indeed, as soon as I began to show interest the militant ecological wing and more threatening sections of The Womens' Society and LGBTQI (I have a suspicion many election posts are only contested by a single candidate because of internal factional bullying and whispering campaigns, mainly originating from these factions, but I have no direct evidence of causation in these cases) began a concerted campaign of what is, in the parlance of certain political operatives, known as 'this.'", 
    para2: "But this is not a story about The Green Party of England and Wales, it is a story of a group of forest animals and their one human companion: myself (Dragon-Bear), Boa-Duck, Lene-Cow and The Master Priest. As I may have alluded to already, I have recently begun an affair with Boa-Duck, and it is with Lene-Cow, my fiancee's, mildly mixed-blessing. She is an anarchist revolutionary and due to her youth and certain affectations about her countenance I had thought her to be naive. She is not. She is slowly developing a network of squatters and upper-middle class drop-outs which she will be using as informants. She also kisses incredibly well, the silk-like seams between her lips opening just a fraction to allow me the merest suggestion of her tongue (I've plagiarised that line from somewhere, but my concern with this piece is far more to inform than to entertain, so you'll have to bear with me).", 
    para3: "Boa-Duck is a fairly small duck, by duck standards. She has subtle, delicate fur that is particularly pleasant to run your hands over and around, though I was careful not to brush too fast or with too much urgency as I am an older dragon-bear and I am stridently aware that the possibility of breaking a much younger duck with overly strong affection is ever-present. This, of course, risks overplaying my hand and making me out to be much more powerful than I am, so for balance I must mention several things about myself. The first is that I am poor, excessively so, that I work for a living creating websites which make no money for me whatsoever, as yet, and exist only on the charity of those who understand my situation. The second, which is a particularly galling consideration considering the first issue, and the occasional flareups of dragon temper and temperament which I am occasionally prone to, is that I have for some considerable time been the victim of a concerted campaign of character assassination which began when I started working on my first, never completed, website: Lake of Birds, but which seems to stem from times further past, from my work in technical support and my ever dwindling length of employment, spurred on by my trade union activism, anarchist disruption campaigns, alcoholism, mental health issues or my increasing inability to keep and hold friends, depending on who you believe (I maintain the first two, as a matter of course and, for my perspective at least, certain and unerring truth).", 
    para4: "Oh yes, and when I searched her bag I found an envelope addressed to the local DCI, in which no letter had yet been placed.", 
    para5: "" 
}; 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    res.render('stories', { title: 'Order Of The Mouse: Phase 1.7 -- Operation Silk Scarf (Narratives)' }); 
}); 

router.get('/BoaDuck/:id', function(req, res, next) { 
    var id = req.params.id; 
    res.render('Boa-Duck/BoaDuckA', { storyText: duckStorySection.para1 }); 
}); 

module.exports = router; 

Im Moment ist es nur die JSON-Daten für para1 lädt, wie man erwarten würde. Was ich brauche ist: wo ich geschrieben habe duckStorySection.para1 Ich würde gerne in die id Variable, so dass, wenn die Adresse eingegeben wird, beispielsweise /BoaDuck/2, die res.render rendert die JSON-Daten entsprechend der 'para2' Schlüssel/Wert-Paar innerhalb duckStorySection. So sollte es wie etwas tun grob:

res.render('Boa-Duck/BoaDuckA', { storyText: duckStorySection.para[id] }) 

mit dem, was nur die korrekte Syntax für die Verrohrung im id Parameter ist. Wie wird das gemacht?

+0

Warum verwenden Sie nicht einfach ein nummernindiziertes Wörterbuchobjekt? –

+0

Ich habe noch nie einen von denen gefunden. Kann ich die Informationen über die korrekte Verwendung in den Express-API-Dokumenten finden oder handelt es sich um Javascript-Sachen? Sind sie wie Python-Wörterbuchobjekte? –

Antwort

0

Vorausgesetzt, dass Ihre Web-Service-API auf numerische Indizes angewiesen ist (/BoaDuck/:id wobei: id eine nicht negative Ganzzahl ist), ist die Verwendung von Schlüsseln wie denen in Ihrem Story-Objekt für Ihr Problem ungeeignet. Gib jedem Eintrag der genauen Schlüssel, dass Sie wollen, dass sie durch abgerufen werden:

var duckStorySection = { 
    0: "I first met Boa-Duck ...", 
    1: "But this is not a story about...", 
    2: "Boa-Duck is a fairly small duck, ...", 
    3: "Oh yes, and when I searched her ...", 
    4: "" 
}; 

Alternativ können Sie einen Array:

var duckStorySection = [ 
    "I first met Boa-Duck ...", 
    "But this is not a story about...", 
    "Boa-Duck is a fairly small duck, ...", 
    "Oh yes, and when I searched her ...", 
    "" 
]; 

Dann nutzen Sie die id Taste direkt über den Wörterbuch:

router.get('/BoaDuck/:id', function(req, res, next) { 
    var id = req.params.id; 
    res.render('Boa-Duck/BoaDuckA', { 
    storyText: duckStorySection[id] 
    }); 
}); 
+0

Ich wollte auch irgendwie einen Variablennamen verwenden, nur damit ich auf einen Blick sehen konnte, was ich tat, aber dieser Code ist wahrscheinlich effizienter. –

+0

Das waren auch keine Variablennamen. 'duckStorySection.Param1' ist gleich 'duckStorySection [' Param1 ']'. Mit nichts anderem kann man viel gewinnen, nicht einmal mit Lesbarkeit. –

1

können Sie einen zusätzlichen Schritt nehmen, also anstatt var id = req.params.id; haben Sie stattdessen var id = 'Para'+req.params.id;, dann können Sieanrufen, das dein Problem beheben würde. Obwohl Sie sich auf Integer-Indizes verlassen, wäre diese Lösung nicht wirklich geeignet. Ich würde vorschlagen, das Objekt nur mit den Ganzzahl-IDs zu indexieren.

Verwandte Themen