TypeError:无法读取未定义的属性“ get”:节点js错误

时间:2020-10-01 12:53:01

标签: javascript node.js

因此,我在Node.js中的内容管理系统上工作,每次启动服务器时,都会收到以下消息:TypeError:无法读取未定义的属性“ get”。

下面的代码在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')
});

有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

您已定义storyblok并使用了Storyblok

s字符应小写。

storyblok.get(`cdn/stories/${path}`...)

答案 1 :(得分:0)

如果您的快速版本为4,则需要指定使用哪个版本,您需要像这样const route = express.Router()来定义路由器。您的代码在版本3中有效。只需确定您的版本即可。