【发布时间】:2020-02-27 16:23:58
【问题描述】:
我有一个这样的js文件:
const {foo} = require('some-lib')
console.log(foo === undefined) // for testing only
module.exports = {
async bar() {
let baz = foo();
}
}
这让我error TypeError: Cannot read property 'foo' of undefined,即使 console.log 打印错误。这里发生了什么?
(实际代码大致是第二个例子here,我正在尝试使用buildQuery函数。)
编辑: 根据请求,这是一个最小的可重现示例:
- 使用 quickstart 设置安装 Strapi (
yarn create strapi-app my-project --quickstart) - 在运行 Strapi 的情况下,转到
localhost:1337/admin,单击边栏中的Content Type Builder,然后创建一个最小的article内容类型。它只需要标题“文章”,不需要其他属性。 - 在
Roles & Permissions中,转到public并全选,然后保存。 - 现在将
my-project/api/article/services/article.js修改为如下所示:
'use strict';
const {buildQuery} = require('strapi-utils');
module.exports = {
find() {buildQuery({model: 'article'})}
};
这会在调用localhost:1337/article 时出现上述错误。请注意,该示例几乎是从 Strapi 的 documentation 中逐字提取的,还要注意 console.log(buildQuery === undefined) 在导入后直接打印 false。
EDIT2:
看来这实际上是 Strapi 中的一个错误,我在他们的 GitHub 存储库上打开了一个问题:https://github.com/strapi/strapi/issues/5306
【问题讨论】:
-
some-lib没有公开任何内容,您可以查看node_modules/some-lib文件夹中的代码 -
some-lib没有名为foo的命名导出。不要试图将代码抽象到无用的程度——所提问题的答案是显而易见的。 -
错误来自
const {foo} = require('some-lib')行。 -
你如何使用这个模块?你导出
bar()函数..不是foo -
如果没有一个最小的、可重现的例子,恐怕很难进一步帮助您。
标签: javascript node.js node-modules strapi