Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将.then()中的值从Node.js模块返回到服务器文件?_Node.js_Asynchronous_Es6 Promise_Node Modules_Fs - Fatal编程技术网

如何将.then()中的值从Node.js模块返回到服务器文件?

如何将.then()中的值从Node.js模块返回到服务器文件?,node.js,asynchronous,es6-promise,node-modules,fs,Node.js,Asynchronous,Es6 Promise,Node Modules,Fs,我正在尝试构建一个模块函数,该函数将使用Sharp调整传入其中的图像的大小。 我将图像数据完美地记录在下面给出的.then()中,但是当我返回同样的结果时,结果却是未定义的 请帮我找出我做错了什么 模块 exports.scaleImg = function (w,h,givenPath){ let toInsertImgData; sharp(givenPath) .resize(w, h) .jpeg({

我正在尝试构建一个模块函数,该函数将使用Sharp调整传入其中的图像的大小。 我将图像数据完美地记录在下面给出的
.then()
中,但是当我
返回同样的结果时,结果却是
未定义的

请帮我找出我做错了什么

模块

exports.scaleImg = function (w,h,givenPath){
          let toInsertImgData;
          sharp(givenPath)
            .resize(w, h)
            .jpeg({
              quality: 80,
              chromaSubsampling: "4:4:4",
            })
            .toFile(compressedImgPath)
            .then(() => {
              fsPromises.readFile(compressedImgPath).then((imgData) => {
                  toInsertImgData = {
                    data: imgData,
                    contentType: "image/jpeg",
                  };
                  console.log(toInsertImgData);
                  return(toInsertImgData);
              });
            });
            
      }
const imageScalingModule = require(__dirname+"/modules.js");



app.post("/compose",upload.fields([{ name: "propic" }, { name: "image" }]),
      (req, res) => {

           console.log(imageScalingModule.scaleImg(640, 480, req.files.image[0].path));
});
这里的
compressedImgPath
只是根目录中文件夹的路径

服务器文件

exports.scaleImg = function (w,h,givenPath){
          let toInsertImgData;
          sharp(givenPath)
            .resize(w, h)
            .jpeg({
              quality: 80,
              chromaSubsampling: "4:4:4",
            })
            .toFile(compressedImgPath)
            .then(() => {
              fsPromises.readFile(compressedImgPath).then((imgData) => {
                  toInsertImgData = {
                    data: imgData,
                    contentType: "image/jpeg",
                  };
                  console.log(toInsertImgData);
                  return(toInsertImgData);
              });
            });
            
      }
const imageScalingModule = require(__dirname+"/modules.js");



app.post("/compose",upload.fields([{ name: "propic" }, { name: "image" }]),
      (req, res) => {

           console.log(imageScalingModule.scaleImg(640, 480, req.files.image[0].path));
});
then()
,因此您需要在
/compose
-处理程序中更改代码,以等待承诺得到解决(我使用的是
async/await
,但您也可以执行
scaleImg(…)。然后()
):