form.parse
跑
异步地
-当你
console.log
外面还没有
parse
d还没有。要么将所有处理新变量的功能都放在回调函数中,要么将回调函数变成承诺并执行
.then
或者把回访变成承诺
await
承诺的决议。
我擅自修复了可能是无意中出现的语法错误
console.log("Inside of parsed method);
和
console.log("Outside of parsed method);
.
async function myFunc() {
var name;
var description;
var price;
var image;
var author = {
id: req.user._id,
username: req.user.username
};
var newCampground = {
name: name,
image: image,
description: description,
author: author,
price: price,
uploadImage: uploadImage
};
var form = formidable.IncomingForm();
await new Promise(resolve => {
form.parse(req, function(err, fields, files) {
newCampground["name"] = fields.name;
newCampground.description = fields.description;
newCampground.price = fields.price;
newCampground.image = fields.image;
console.log("Inside of parsed method");
console.log(JSON.stringify({
newCampground
}));
resolve();
});
});
console.log("Outside of parsed method");
console.log(JSON.stringify({
newCampground
}));
}
myFunc();