首页 > 解决方案 > Gatsby Config 在 Javascript 文件中给出错误??操作员

问题描述

我正在尝试运行 Gatsby 网站,但是当我这样做时npm run develop,我收到了这个错误:

> gatsby develop

 ERROR #10123  CONFIG

We encountered an error while trying to load your site's gatsby-config. Please fix the error x try again.



  Error: D:\Coding\web-blog\src\infra\feed.js:28
  description: post.intro ?? post.description,
                           ^

代码:

const item = {
                title: sanitizeTitle(post.title),
                description: post.intro ?? post.description,
                url: `${site.siteMetadata.siteUrl}/${post.slug}`,
                guid: `${site.siteMetadata.siteUrl}/${post.slug}`,
                categories: post.tags,
                author: site.siteMetadata.author,
                date: post.date,
            }

什么是JavaScript 中的??运算符?它是一种新的语言功能吗?

标签: javascriptgatsbygatsby-plugin

解决方案


nullish 合并运算符 ( ??) 它是 ECMAScript 2020 中的一项功能(请参阅 TC39 提案)。您可以通过修改 babel 配置或安装gatsby-plugin-nullish-coalescing-operator插件来添加它。

如果您选择第一个版本,.babelrc请在项目的根目录中添加 a 并使用以下内容填充它:

{
  "plugins": [
    ["@babel/plugin-proposal-nullish-coalescing-operator"]
  ],
  "presets": [
    [
      "babel-preset-gatsby",
      {
        "targets": {
          "browsers": [">0.25%", "not dead"]
        }
      }
    ]
  ]
}

注意:您需要安装@babel/plugin-proposal-nullish-coalescing-operator依赖项。

Javascript 中的这个运算符是什么?

如果 left 是or , nullish 合并运算符 ( ??) 使用正确的值。OR 运算符 ( ) 之间的主要区别在于假值,在,或值中使用时可能会导致一些错误(还有更多的假值,例如,等,但这些是常见的)。所以:nullundefined||""0false-0NaN

description: post.intro ?? post.description,

post.intro只要它存在,您就会使用,否则(如果post.intronullundefined),您将使用post.description.


推荐阅读