【发布时间】:2019-06-07 02:06:31
【问题描述】:
有人可以解释exports部分中的部分吗,我似乎迷失了一段时间。从importPromise开始。似乎有很多事情发生,例如箭头函数和地图方法。我看不到数据从哪里流向哪里。
const keystone = require('keystone');
const PostCategory = keystone.list('PostCategory');
const Post = keystone.list('Post');
const importData = [
{ name: 'A draft post', category: 'Keystone JS' },
...
];
exports = function (done) {
const importPromise = importData.map(({ name, category }) => createPost({ name, category }));
importPromise.then(() => done()).catch(done);
};
const categories = {};
const createPost = ({ name, category }) => {
let postCategory = new PostCategory.model({ category });
if (categories[category]) {
postCategory = categories[category];
}
categories[category] = postCategory;
const post = new Post.model({ name });
post.category = postCategory._id.toString();
return Promise.all([
post.save(),
postCategory.save()
]);
}
【问题讨论】:
-
您在哪里找到该代码?无论如何,它似乎不起作用 - 在该
exports函数中缺少Promise.all调用 -
所以您知道箭头函数是什么以及它是如何工作的,您知道
map是什么以及做什么?那么解构和对象字面量这行的哪一部分你不明白呢? -
@Bergi 这是一个keystone应用程序,代码段来自link。我几乎迷路了,因为我对编程很陌生,这就是为什么我真的不知道东西去哪里了。是的,我学习了箭头函数和地图,但是将它们结合在一起让我很困惑。我也不熟悉解构和对象文字实践......
标签: javascript ecmascript-5 keystonejs