JavaScript:任何人都可以让它工作或解释为什么它不工作吗?
JavaScript: Can anyone get this to work or explain why it doesn't?
更新
根据 epiqueras 的回答,我查看了我是如何处理导入的。首先,/models/index.js
正在导出命名导出。这是代码:
'use strict';
import { readdirSync } from 'fs'
import {
basename,
extname,
resolve
} from 'path';
//
// Using module.exports here as a bit of a hack
// to allow for member imports in the form of
// import { Constructor } from "~/models"
module.exports = readdirSync(__dirname) // Get contents of current directory
.filter(f => f !== 'index.js') // Exclude the index file
.map(f => resolve(__dirname, f)) // Resolve the complete module file path
.map(f => require(f).default) // Require the module
.reduce((prev, next) => { // Reduce the array of modules to a hash of Module.name = Module
prev[next.name] = next;
return prev;
}, {});
我从对我不起作用的 requireindex 项目中得出这个(毫无疑问是用户错误)。我后来发现,如果我直接导入 class,即 import Patron from '../models/patron'
,那么一切都会按预期工作。
此时,我的项目中还有五个其他模型都可以使用上面的代码正常导出。 Patron
是唯一没有的。正如原始问题中所述,如果我将名称更改为其他任何名称,上面的代码将毫无问题地导出该新名称。
谢天谢地,我现在有了解决方法。希望我能弄清楚为什么它会因为名字 Patron
.
而窒息
原题
我在JavaScript中写了一个简单的class:
'use strict'
export default class Patron {
constructor(props) {
this.props = props;
}
create() {
// In my actual code I make a network call,
// simplified this just to see if anyone can get it to return a promise
return Promise.resolve(this);
}
}
为了完整起见,下面是我如何使用构造函数的示例:
'use strict'
import { Router } from 'express';
import { Patron } from '../models';
const PatronRouter = Router();
PatronRouter.post('/patrons', (req, res) => {
let patron = new Patron({ name: 'John Doe' });
let promise = patron.create().then(response => res.send(response);
}
export PatronRouter;
这是我的经历:
Patron
是一个有效的构造函数,初始化实例时没有错误
patron
是 Patron
的实例
patron.props
是 undefinded
patron.create
作为实例方法存在
patron.create
returns undefined
这对我来说完全没有意义:如果我更改 class 的名称一切正常。 我不理解 where/how/why name Patron
导致了问题?
一些其他注意事项:
- 运行 节点 (6.9.2)
- Express(最新)应用程序的一部分,试图从
Router
执行此代码
- 使用启用了
es2015
预设的 Babel 6
想法?
您将 class 导出为默认导出,但将其导入为命名导出。
试试这个:import Patron from '../models
;
或者将导出更改为命名导出:export class Patron
更新
根据 epiqueras 的回答,我查看了我是如何处理导入的。首先,/models/index.js
正在导出命名导出。这是代码:
'use strict';
import { readdirSync } from 'fs'
import {
basename,
extname,
resolve
} from 'path';
//
// Using module.exports here as a bit of a hack
// to allow for member imports in the form of
// import { Constructor } from "~/models"
module.exports = readdirSync(__dirname) // Get contents of current directory
.filter(f => f !== 'index.js') // Exclude the index file
.map(f => resolve(__dirname, f)) // Resolve the complete module file path
.map(f => require(f).default) // Require the module
.reduce((prev, next) => { // Reduce the array of modules to a hash of Module.name = Module
prev[next.name] = next;
return prev;
}, {});
我从对我不起作用的 requireindex 项目中得出这个(毫无疑问是用户错误)。我后来发现,如果我直接导入 class,即 import Patron from '../models/patron'
,那么一切都会按预期工作。
此时,我的项目中还有五个其他模型都可以使用上面的代码正常导出。 Patron
是唯一没有的。正如原始问题中所述,如果我将名称更改为其他任何名称,上面的代码将毫无问题地导出该新名称。
谢天谢地,我现在有了解决方法。希望我能弄清楚为什么它会因为名字 Patron
.
原题
我在JavaScript中写了一个简单的class:
'use strict'
export default class Patron {
constructor(props) {
this.props = props;
}
create() {
// In my actual code I make a network call,
// simplified this just to see if anyone can get it to return a promise
return Promise.resolve(this);
}
}
为了完整起见,下面是我如何使用构造函数的示例:
'use strict'
import { Router } from 'express';
import { Patron } from '../models';
const PatronRouter = Router();
PatronRouter.post('/patrons', (req, res) => {
let patron = new Patron({ name: 'John Doe' });
let promise = patron.create().then(response => res.send(response);
}
export PatronRouter;
这是我的经历:
Patron
是一个有效的构造函数,初始化实例时没有错误patron
是Patron
的实例
patron.props
是undefinded
patron.create
作为实例方法存在patron.create
returnsundefined
这对我来说完全没有意义:如果我更改 class 的名称一切正常。 我不理解 where/how/why name Patron
导致了问题?
一些其他注意事项:
- 运行 节点 (6.9.2)
- Express(最新)应用程序的一部分,试图从
Router
执行此代码
- 使用启用了
es2015
预设的 Babel 6
想法?
您将 class 导出为默认导出,但将其导入为命名导出。
试试这个:import Patron from '../models
;
或者将导出更改为命名导出:export class Patron