2016-04-13 21 views
2

Görünüm motoru olarak express-handlebars ile Express framework kullanarak bir nodejs uygulaması oluşturmaya çalışıyorum ama görüntülemeye çalışırken aşağıdaki hata ile sıkışmış ettik ana sayfa: burada yanlış ne oluyorExpress-Handlebars görünüm motoru kullanarak Express 4.x çerçevesinde kısmi bir görünüm oluşturma

Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed [object Object]

kimse bana bildirebilirsiniz. github repo

linki:

var express = require('express'), 
    app = express(), 
    hbs = require('express-handlebars'), 
    // Create `ExpressHandlebars` instance with a default layout. 
    hbsInstance = hbs.create({ 
     defaultLayout: 'main', extname: '.hbs' 
    }), 
    fortune = require('./lib/fortune.js'), 
    weatherData = require('./lib/weather.js'); 

app.engine('hbs', hbsInstance.engine); 
app.set('view engine', 'hbs'); 
app.set('port', process.env.PORT || 3000); 
app.use(express.static(__dirname + '/public')); 

app.use(function(req, res, next) { 
    if (!res.locals.partials) { 
     res.locals.partials = {}; 
    } 

    res.locals.partials.weather = weatherData.getWeatherData(); 
    next(); 
}); 

app.get('/', function(req, res) { 
    res.render('home'); 
}); 

app.listen(app.get('port'), function() { 
    console.log('Express started on http://localhost:' + app.get('port')); 
}); 
bu yerleştirilir weather.js

index.js: Aşağıda https://github.com/bdinesh/LearningNode.git

ben yönetmeye çalışıyorum kodudur lib klasörü

exports.getWeatherData = function getWeatherData() { 
    return { 
     locations: [ 
      { 
       name: 'Portland', 
       forecastUrl: 'http://www.wunderground.com/US/OR/Portland.html', 
       iconUrl: 'http://icons-ak.wxug.com/i/c/k/cloudy.gif', 
       weather: 'Overcast', 
       temp: '54.1 F (12.3 C)', 
      }, 
      { 
       name: 'Bend', 
       forecastUrl: 'http://www.wunderground.com/US/OR/Bend.html', 
       iconUrl: 'http://icons-ak.wxug.com/i/c/k/partlycloudy.gif', 
       weather: 'Partly Cloudy', 
       temp: '55.0 F (12.8 C)', 
      }, 
      { 
       name: 'Manzanita', 
       forecastUrl: 'http://www.wunderground.com/US/OR/Manzanita.html', 
       iconUrl: 'http://icons-ak.wxug.com/i/c/k/rain.gif', 
       weather: 'Light Rain', 
       temp: '55.0 F (12.8 C)', 
      }, 
     ], 
    }; 
}; 

weather.hbs (Kısmen) bu, görünümler/kısmi klasörler klasörüne yerleştirilir. görünümlerinde yerleştirilen

<div class="weatherWidget"> 
    {{#each partials.weather.locations}} 
    <div class="location"> 
     <h3>{{name}}</h3> 
     <a href="{{forecastUrl}}"> 
      <img src="{{iconUrl}}" alt="{{weather}}"> 
      {{weather}}, {{temp}} 
     </a> 
    </div> 
    {{/each}} 
    <small>Source: <a href="http://www.wunderground.com">Weather Underground</a></small> 
</div> 

home.hbs şöyle

<h1>Welcome to Meadowlark travel</h1> 
{{> weather}} 

cevap

1

kodunuzu değiştirin klasörü:

<div class="weatherWidget"> 
{{#each partials.weatherData.locations}} 
<div class="location"> 
    <h3>{{name}}</h3> 
    <a href="{{forecastUrl}}"> 
     <img src="{{iconUrl}}" alt="{{weather}}"> 
     {{weather}}, {{temp}} 
    </a> 
</div> 
{{/each}} 
<small>Source: <a href="http://www.wunderground.com">Weather Underground</a></small> 

maalesef bazı eski parça ve hatalar içinde vardır inanılmaz Web Düğüm ve Ekspres ile Geliştirme.

doğrularını için buraya bakınız: sorun için buraya http://www.oreilly.com/catalog/errata.csp?isbn=0636920032977 Ve sahiptirler: https://github.com/EthanRBrown/web-development-with-node-and-express/issues/28

İlgili konular