【问题标题】:Get value from function then make it global从函数中获取价值,然后使其成为全局
【发布时间】:2019-06-04 07:56:05
【问题描述】:

如何从函数内部获取值以在将数据发布到数据库时使用它?在这里,我想将multer.diskStorage 中的 imageName 的值设为全局,以便在发布数据库路径时使用它

var imageName;
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/assets/images');
    },
    filename: function (req, file, cb) {
        if (!file.originalname.match(/\.(png|jpg|jpeg)$/)) {
            var err = new Error();
            err.code = 'filetype';
            return cb(err);
        } else {
            imageName = Date.now() + '_' + file.originalname;
            cb(null, imageName);
        }
    }
});

router.post('/newsfeeds', parser.single("myfile"), function (req, res) {

    newsfeed.path = imageName;
    newsfeed.save(function (err) {
    });
});

【问题讨论】:

  • 当您点击/newsfeeds 端点时,您的var storage = multer.diskStorage({ ... 是否会执行?
  • 暂时未定义
  • 那么您的var storage = multer.diskStorage({...}) 代码会在router.post(/newsfeeds....) 被调用之前执行吗?
  • 其实这是我的问题,newsfeeds 被先调用然后 multer
  • 然后尝试创建一个执行 multer 部分的函数,然后 return imageName 来自新函数的变量或值。然后在您的新闻提要块中调用该函数。

标签: node.js mongodb mean-stack multer


【解决方案1】:

在编程社区中,全局变量往往是不受欢迎的,所以你应该先问问自己这是否真的是你想要做的。

如果您确定这是最好的方法,但是,在 Node 中,您可以将属性附加到 global 变量。在浏览器中,您可以将其附加到window

至于您的执行顺序问题(来自 OP 的 cmets),您可以在全局范围内添加 Promise 而不是值。然后,只要您想要该值,就您的值调用.then(),或者在async functionawait 中运行它。

【讨论】:

    【解决方案2】:

    这是实现multer 的一种方式。您可以将upload 用作函数或中间件,我将其用作函数是因为它可以让我更详细地处理错误。

    不需要全局变量,在这种情况下,在 multer 处理图像的那一刻,用图像的信息填充 req.file 属性,因为我们现在在 req.file.filename 中覆盖了函数 filename 这是我们需要的自定义值。

    const multer = require('multer');
    
    const multerStorage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, './public/assets/images');
        },
        filename: function (req, file, cb) {
            if (!file.originalname.match(/\.(png|jpg|jpeg)$/)) {
                var err = new Error();
                err.code = 'filetype';
                return cb(err);
            } else {
                const imageName = Date.now() + '_' + file.originalname;
                cb(null, imageName);
            }
        }
    });
    
    const upload = multer({ storage: multerStorage }).single('myfile');
    
    
    router.post('/newsfeeds',(req, res) => {
        upload(req, res, function (err) {
            if (err || !req.file) {
                // An error occurred while loading the image.
                return res.status(400);
            }
            newsfeed.path = req.file.filename;
            newsfeed.save(function (err) {
            });
        });
    });
    

    您可以在此处找到包含图像其余属性的表格 在multer 处理图像之后您拥有的。 Documentation

    【讨论】:

    • 仍然不起作用,它将图像保存到文件夹但我的数据库中没有输出
    • @Godshand 在您发布的代码中,您没有为 newsfeed 声明值,这可能是问题所在,但您的问题已通过我的代码获取图像名称来解决req.file.filename 无需全局变量。
    • @Godshand 我已经用图片myfile的输入字段名称更新了我的答案,希望对您有所帮助。
    • @Godshand 如果我的回答对您有用,您可以接受或限定它,以便其他有类似问题的用户可以更快地找到解决方案。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    相关资源
    最近更新 更多