【问题标题】:im getting this error refrence error in node.js. Can someone explain the source of this serror我在 node.js 中收到此错误参考错误。有人可以解释这个错误的来源吗
【发布时间】:2021-04-21 08:02:02
【问题描述】:

{url: ${api}/products , 方法: ['GET', 'OPTIONS']}, ReferenceError: api 未在 authJwt (C:\Users\abuba\Desktop\BACKEND\helpers\jwt.js:9:22) 中定义 const expressJwt = require('express-jwt');

function authJwt(){
    const secret = process.env.secret;
    return expressJwt({
        secret,
        algorithms: ['HS256']
    }).unless({
        path: [
            {url: `${api}/products` , methods: ['GET', 'OPTIONS']},
            `${api}/users/login`,
            `${api}/users/register`

        ]
    })
}


module.exports = authJwt;

【问题讨论】:

  • 一切都在错误信息中。变量/常量api没有定义,需要定义才能使用。

标签: node.js


【解决方案1】:

您需要定义一些名为api 的参数。 喜欢

const api = 'my-endpoint/sample'

无论何时使用模板字符串,你都必须在使用时定义参数这样使用 ${},可以阅读here关于模板字符串的信息。

所以,你可以像这样使用它:

const api = 'my-endpoint/sample'
url: `${api}/products`

这将返回网址为:

url = 'my-endpoint/sample/products'

【讨论】:

  • 它帮助了很多干杯伙伴
  • 当然,如果这回答了你的问题,你可以接受它作为答案或投票:')。很高兴能帮助你,干杯。 @Abubakarkhalid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 2012-08-24
相关资源
最近更新 更多