所以我正在使用Nodejs开发一个内容管理系统,每当我启动服务器的时候,我都会收到这样的信息: TypeError: Cannot read property ' get‘of undefined。
下面的代码在index.js文件中。
'use strict';
const express = require('express');
const exphnbars = require('express-handlebars');
const url = require('url');
const { default: Storyblok } = require('storyblok-js-client');
const app = express();
//....
const storyblokClient = require('storyblok-js-client')
let storyblok = new storyblokClient({
accessToken: 'J0irYFbngEQ6ZFlRqs6llwtt'
});
// ----
app.get('/*', function(req, res){
var path = url.parse(req.url).pathname;
path = path == '/'? 'home' : path;
Storyblok
.get(`cdn/stories/${path}`,{
version: req.query.storyblok ? 'draft': 'published'
})
.then((response) => {
res.render('index', {
story: response.data.story
});
})
.catch((error) => {
res.send(error);
});
});
app.use('/public', express.static('public'));
//telmplate engine
app.engine('.hbs', exphnbars({
defaultLayout: 'main',
extname: '.hbs',
partialsDir: 'views/components/'
}));
app.set('view engine', '.hbs');
app.set('views', '.hbs');
app.listen(5050, function(){
console.log('Example app listening on port 5050')
});有什么需要帮忙的吗?
发布于 2020-10-01 20:58:26
您已经定义了storyblok并使用了Storyblok
字符s应为小写。
storyblok.get(`cdn/stories/${path}`...)发布于 2020-12-16 18:13:01
您需要指定您使用的版本如果您的express版本是4,您需要像这样定义您的路由器const route = express.Router()。你的代码可以在版本3中工作。只需确定你的版本。
https://stackoverflow.com/questions/64155822
复制相似问题