Render json-Objekt mit express und jade - kein Zugriff auf json-Felder

Relevanten Express-Teil des Knotens Anwendung:

/*Route to Product Views*/
app.get('/product/:id', function(req, res){
        Product.find({_id: req.params.id}, function (error, data) {
                if(error){
                        console.log(error);
                } else {
                        console.log("DATA :" + data); //correct json object
                        res.render('product',{
                                title: 'Product Template',
                                result: data
                                }
                        );
                }
        });

});

Jade-Vorlage:

!!! 5
html
  head
    title #{title}
  body
    h1 #{result.name}
    h2 #{result.unitprice}
    p.
       #{result.description}
    h3 #{result}

Also, wenn ich unter http://myhost.com/product/51fa8402803244fb12000001 alles, was ich sehe ist die Ausgabe des h3 #{Ergebnis}, das ist:

[{ 
__v: 0, 
_id: 51fa8402803244fb12000001, 
description: 'Awesome stuff you really need', 
discontinued: false, 
name: 'Some product', 
unitprice: 5.99 
}]

Verwendung von JSON.stringify macht keinen Unterschied außer, dass h3 #{Ergebnis} gibt "stringified" JSON.
Wie man richtig auf die Felder von den json-string?

Bruder, wenn result ist eine single-document-array, sollten Sie nicht verwenden result[0].name, result[0].unitprice? Was bedeutet console.log drucken?
Danke Bro, funktioniert es wie, wenn JSON.stringify () - ist nicht verwendet!

InformationsquelleAutor NorRen | 2013-08-02

Schreibe einen Kommentar