我被困在代码中我尝试了不同的方式来等待 node.js 中的写入文件,但它没有等待。下面是代码app.post('/createEmp',async (req,res) => { console.log("Inside createEmp()"); var response = await createEmp(req.body); console.log(response);}); async function createEmpFile(fileContentObj){ var response = {}; console.log("filePath :"+filePath); return new Promise(function(resolve,reject){ fs.writeFile(filePath, fileContentStr, function (err) { if (err){ console.log("err :"+err); reject(err); } else { console.log('Saved!'); fileSavedFlag = true; response.result = fileSavedFlag; console.log(response); resolve(response) }; }); }); // end of promise /*return new Promise(function(resolve,reject) { const file = fs.createWriteStream(filePath) file.write(fileContentStr) file.end(); file.on("finish", () => { fileSavedFlag = true; resolve(response) }); file.on("error", () => { resolve(response) }); }); */}在节点 js 控制台上,我可以看到响应,但是当我使用邮递员调用时,我没有得到任何响应
1 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
你的帖子路线应该res.send如下
app.post('/createEmp',async (req,res) => {
console.log("Inside createEmp()");
var response = await createEmp(req.body);
console.log(response);
res.send(response)
});
添加回答
举报
0/150
提交
取消