如何等到节点的回调函数执行完毕
How to wait until the node's call back function executed
我正在尝试使用节点上传 file/store 并将文件保存在 Gird FS 中。下面是代码示例。上传功能完美运行。由于节点的异步特性,响应会立即发送回浏览器。我想保留响应直到执行回调函数。
注意:我也尝试在回调函数中发送最终响应,但仍然没有用。
请说出一些想法。
const multer = require("multer");
const GridFsStorage = require("multer-gridfs-storage");
const mongoose = require("mongoose");
mongoose.connect(CONFIG.dbConfig, {useNewUrlParser: true, useUnifiedTopology: true} );
var storage = new GridFsStorage({
url: CONFIG.dbConfig,
options: { useNewUrlParser: true, useUnifiedTopology: true },
file: (req, file) => {
return {
bucketName: "xyz",
filename: `${file.originalname}`
};
}
});
const viewPage = (req, res) => {
try {
var uploadFile = multer({ storage: storage }).single("file");
uploadFile(req, res, function(err) {
if (req.file == undefined) {
return res.send(`You must select a file.`);
}
/* code to save anthother Schema */
fileDetails.save(function(err) {
if (err) throw err;
});
});
return res.send(`File has been uploaded.`);
} catch (error) {
console.log(error);
return res.send(`Error when trying upload image: ${error}`);
}
};
module.exports = {
getPage: viewPage
};
您将不得不使用 async 和 await
// convert multer function to async await
function uploadFile(req) {
return new Promise(resolve => {
var uploadFile = multer({ storage: storage }).single("file");
if (req.file == undefined) {
return resolve([`You must select a file.`, null]);
}else {
// do something
return resolve([null, data])
}
});
}
const viewPage = async (req, res) => {
try {
let [err, data] = await uploadFile(req)
// check error and send response
// convert do async await similarly for below fileDetails.save
/* code to save anthother Schema (change this to async await) */
fileDetails.save(function(err) {
if (err) throw err;
});
});
return res.send(`File has been uploaded.`);
} catch (error) {
console.log(error);
return res.send(`Error when trying upload image: ${error}`);
}
};
我正在尝试使用节点上传 file/store 并将文件保存在 Gird FS 中。下面是代码示例。上传功能完美运行。由于节点的异步特性,响应会立即发送回浏览器。我想保留响应直到执行回调函数。 注意:我也尝试在回调函数中发送最终响应,但仍然没有用。
请说出一些想法。
const multer = require("multer");
const GridFsStorage = require("multer-gridfs-storage");
const mongoose = require("mongoose");
mongoose.connect(CONFIG.dbConfig, {useNewUrlParser: true, useUnifiedTopology: true} );
var storage = new GridFsStorage({
url: CONFIG.dbConfig,
options: { useNewUrlParser: true, useUnifiedTopology: true },
file: (req, file) => {
return {
bucketName: "xyz",
filename: `${file.originalname}`
};
}
});
const viewPage = (req, res) => {
try {
var uploadFile = multer({ storage: storage }).single("file");
uploadFile(req, res, function(err) {
if (req.file == undefined) {
return res.send(`You must select a file.`);
}
/* code to save anthother Schema */
fileDetails.save(function(err) {
if (err) throw err;
});
});
return res.send(`File has been uploaded.`);
} catch (error) {
console.log(error);
return res.send(`Error when trying upload image: ${error}`);
}
};
module.exports = {
getPage: viewPage
};
您将不得不使用 async 和 await
// convert multer function to async await
function uploadFile(req) {
return new Promise(resolve => {
var uploadFile = multer({ storage: storage }).single("file");
if (req.file == undefined) {
return resolve([`You must select a file.`, null]);
}else {
// do something
return resolve([null, data])
}
});
}
const viewPage = async (req, res) => {
try {
let [err, data] = await uploadFile(req)
// check error and send response
// convert do async await similarly for below fileDetails.save
/* code to save anthother Schema (change this to async await) */
fileDetails.save(function(err) {
if (err) throw err;
});
});
return res.send(`File has been uploaded.`);
} catch (error) {
console.log(error);
return res.send(`Error when trying upload image: ${error}`);
}
};