【问题标题】:TypeError: Cannot read property 'get' of undefined: A node js errorTypeError:无法读取未定义的属性“get”:节点 js 错误
【发布时间】:2021-01-17 04:51:40
【问题描述】:

所以我正在使用 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')
});

有什么帮助吗??

【问题讨论】:

  • Storyblok 未定义,因为你用小写的storyblok 定义了它,修正错字,你应该会很好

标签: javascript node.js


【解决方案1】:

你已经定义了storyblok并使用了Storyblok

字符s应该是小写的。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 2018-11-30
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多