代码之家  ›  专栏  ›  技术社区  ›  kjunz

函数内的全局值不变

  •  1
  • kjunz  · 技术社区  · 7 年前

    我正在使用强大解析一个包含文本和上传图像的传入表单。但是,我无法使用表单中解析的值更新这些全局变量(name、description等)。parse()方法。

    如果我控制台。将NewCampGrand对象记录在表单中。方法,每个字段值都会正确保存。但只要我一安慰。在parse方法之外记录相同的新Campground对象,该对象为空。我花了2个小时试图解决这个问题,但我无法让它工作。任何帮助都将不胜感激!

      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();
    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}));//this one has everything
    });
    console.log("Outside of parsed method);
    console.log(JSON.stringify({newCampground}));//nothing inside
    
    // ===============console output==================//
    Outside of parsed method
    {"newCampground":{"author":{"id":"5ab8893a4dd21b478f8d4c40","username":"jun"}}}
    Inside of parsed method
    {"newCampground":{"name":"aaaaa","image":"","description":"ddddd","author":{"id":"5ab8893a4dd21b478f8d4c40","username":"jun"},"price":"vvvvv","uploadImage":"/uploads/i.jpg"}}
    { author: { id: 5ab8893a4dd21b478f8d4c40, username: 'jun' },
      comments: [],
      _id: 5ac4164432f6902a2178e877,
      __v: 0 }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   CertainPerformance    7 年前

    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();