首页 > 解决方案 > 使用函数参数声明 req.body.parameter

问题描述

如果我使用不同的参数调用它,我想创建一个函数,它应该使用参数来定义一个 req.body 以便在视图中使用。不好的描述,我会说明我的意思:这个函数

function savePaintingsAll(post, profilpictureEncoded,paintingsname,paintingstype) {
    if (profilpictureEncoded == null) return;
    const profpic = JSON.parse(profilpictureEncoded);
    if (profpic != null && imageMimeTypes.includes(profpic.type)) { // If the file is a json obj & from the type image (jpg, png)
        const name = paintingsname;
        const type = paintingstype;
        post.name = new Buffer.from(profpic.data, 'base64')    // Buffer.from(where, how)
        post.type = profpic.type
    }
}

我希望参数paintingname 和paintingtype 来定义我是否调用savePaintingsAll(post, req.body.paintings3, paintings3, paintingsType3),但是如果我这样做了,那么如果我将它们放在if 语句中或之外,则参数不会在函数ether 中使用。问题出在哪里?我用ejs,快递。我需要使用数据在数据库中保存不同的绘画并显示它们:

<img id="painting" class="" style="max-width: 20%; min-width: 300px; height: auto;"src="<%= post.paintingsP %>" />
    <img id="painting2" class="" style="max-width: 20%; min-width: 300px; height: auto;"src="<%= post.paintingsP2 %>" />

标签: javascriptnode.jsejs

解决方案


推荐阅读