我最近被介绍给Promises。我需要将异步操作转换为Promise的帮助。这是代码:
var data = [
{
name: "Brandt",
image: "img.jpg",
description: "Lorem ipsum"
},
{
name: "Paul",
image: "img2.jpg",
description: "ipsum"
},
{
name: "Jim",
image: "img3.jpg",
description: "Lorem"
},
];
function seedDB(){
// remove all campgrounds
Campground.remove({}, function(err){
if(err){
console.log;
} else {
console.log("campgrounds removed!");
}
// remove all comments
Comment.remove({}, function(err){
if (err) {
console.log(err);
}
else {
console.log("comments removed");
}
// add a few campgrounds
data.forEach(function(seed){
Campground.create(seed, function(err, campground){
if (err){
console.log(err);
} else {
console.log("added a campground");
// add a few comments
Comment.create({
text: "lorem ipsum.",
author: "Homer"
}, function(err, comment){
if (err){
console.log(err);
}
else {
campground.comments.push(comment);
campground.save();
console.log("created new comment");
}
});
}
});
});
});
});
}
这是我尝试过的:
var seedDB = promisify(Campground.remove);
seedDB({})
.then(() => promisify(Comment.remove({}))
)
.then(() =>
data.forEach(function(seed){
Campground.create(seed, function(err, campground){
if (err){
console.log(err);
} else {
console.log("added a campground");
// add a few comments
Comment.create({
text: "lorem ipsum.",
author: "Homer"
}, function(err, comment){
if (err){
console.log(err);
}
else {
campground.comments.push(comment);
campground.save();
console.log("created new comment");
}
})
}
})
})
)
.catch((err)=>console.log(err))
我正在使用MongoDB和Express。我的尝试引发以下情况:
TypeError: Cannot read property 'Query' of undefined
at remove (/home/ubuntu/workspace/yelpcamp/v6/node_modules/mongoose/lib/model.js:1697:23)
at remove (internal/util.js:230:26)
at Object.<anonymous> (/home/ubuntu/workspace/yelpcamp/v6/seeds.js:77:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/ubuntu/workspace/yelpcamp/v6/index.js:13:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
我知道Promise.all(arr)
接受包含一堆异步操作的数组。我可以有这样一个数组,但是对于forEach回调我该怎么办?
这些是我的进口商品:
const { promisify } = require("util")
const Promise = require("bluebird")
答案 0 :(得分:0)
我已经在朋友的帮助下制定了解决方案。如果有人遇到类似问题,我会发布。在这里:
async function seedDB(){
try{
await Campground.remove({});
console.log("campgrounds removed!");
await Comment.remove({});
console.log("comments removed")
await User.remove({});
console.log("users removed");
let user = await User.create({username: 'Stacy', password: 'password'});
for(let d of data){
let campground = await Campground.create(d);
console.log("added campground");
let comment = await Comment.create({text: "hello", author:user._id}); // this is required because the Comment schema ({text: String, author: ref -> User}) stores a reference to User.
campground.comments.push(comment);
await campground.save();
console.log("comments saved");
}
} catch(err){
console.log(err);
}
}