我正在遵循有关Javascript的教程-当前正在构建一个涉及使用类的应用程序,我遇到了一个错误,该错误的标题为“未捕获的TypeError:_models_Search__WEBPACK_IMPORTED_MODULE_0 ___ default.a不是构造函数” 这个项目也涉及使用MVC设计模式。
我试图从'./models/Search'重命名导入搜索;从“ ./models/Search”导入{Search};但是仍然收到以下完整的错误消息:
Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
at eval (index.js:5)
at Module../src/js/index.js (bundle.js:4198)
at __webpack_require__ (bundle.js:20)
at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:3:18)
at Object.0 (bundle.js:4220)
at __webpack_require__ (bundle.js:20)
at bundle.js:84
at bundle.js:87
Search.js文件
import axios from 'axios';
export default class Search {
constructor(query) {
this.query = query;
}
async getResults() {
const proxy = 'https://cors-anywhere.herokuapp.com/'; const
key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
try {
const res = await
axios(`${proxy}https://www.food2fork.com/api/search?
key=${key}&q=${this.query}`);
this.result = res.data.recipes;
console.log(recipes);
} catch (error) {
alert(error);
}
}
}
index.js文件
import Search from './models/Search';
const search = new Search('pizza');
console.log(search);
预期结果是要在客户端中返回搜索属性,并且我希望能够在浏览器中访问其所有属性。是的,如果您想知道的话,我故意x出了API密钥-如果给出答案,实际上也会有所帮助,您可以简要地了解我出错的地方。预先感谢。
答案 0 :(得分:0)
要正确导入Search类,请使用以下几行。 在index.js中:
with mytable as (
select 1 id, '[
{ "date": "2019-01-01", "value": 1, "othervaluesidontneed": 12 },
{ "date": "2019-01-02", "value": 2, "othervaluesidontneed": 55 },
{ "date": "2019-01-03", "value": 3, "othervaluesidontneed": 72}
]' js
)
select
json_arrayagg(json_object( 'date', tt.date, 'value', tt.value)) new_js
from
mytable t,
json_table(
js,
"$[*]"
columns(
date datetime path "$.date",
value int path "$.value"
)
) as tt
group by t.id
在搜索类声明中:
import Search from './models/Search';
您尝试过的或第二种方式(在导入中带有大括号): 在index.js
export default class Search
然后导出,没有默认:
import { Search } from './models/Search';
仅在命名的出口中需要大括号