Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Node.js 如果表单提交中未选择任何图像,如何设置默认图像_Node.js_Express_Mongoose_Cloudinary - Fatal编程技术网

Node.js 如果表单提交中未选择任何图像,如何设置默认图像

Node.js 如果表单提交中未选择任何图像,如何设置默认图像,node.js,express,mongoose,cloudinary,Node.js,Express,Mongoose,Cloudinary,我有一个用node.js、express和mongoose构建的CRUD应用程序。在这个应用程序中,我有一个表单,其中一个输入是用于上传图像的文件输入。我正在使用cloudinary、cloudinary存储和multer来处理这些图像。如果用户上传图像,我可以成功地将其添加到我的数据库中,并随后进行渲染。我没有弄清楚的是,如果在文件输入中没有选择任何图像,如何设置默认图像。这是我提交的表格: router.post('/add', parser.single("image"), (req, r

我有一个用node.js、express和mongoose构建的CRUD应用程序。在这个应用程序中,我有一个表单,其中一个输入是用于上传图像的文件输入。我正在使用cloudinary、cloudinary存储和multer来处理这些图像。如果用户上传图像,我可以成功地将其添加到我的数据库中,并随后进行渲染。我没有弄清楚的是,如果在文件输入中没有选择任何图像,如何设置默认图像。这是我提交的表格:

router.post('/add', parser.single("image"), (req, res) => {
    const newContent = {
        name: req.body.contentName,
        userId: req.user.id,
        username: req.user.username,
        image: {
            url: req.file.url,
            id: req.file.public_id
        },
    }
    new Content(newContent)
    .save()
    .then(() => {
        req.flash('success_msg', 'Content added');
        res.redirect('/content')
    })
});
这是我的cloudinary配置:

cloudinary.config({
    cloud_name: ******,
    api_key: ******,
    api_secret: ******
    });
    const storage = cloudinaryStorage({
    cloudinary: cloudinary,
    folder: "demo",
    allowedFormats: ["jpg", "png"],
    transformation: [{ width: 348, height: 236.81, crop: "limit" }]
    });
    const parser = multer({ storage: storage });
我查阅了cloudinary文档,发现了一些要添加的“default_image”参数的实例,但无论我在cloudinary配置中的何处添加它,都会收到错误消息无法读取未定义的属性“url”。这应该在我的帖子中、在我的配置中处理,还是两者都要处理?作为参考,“我的内容模型”的图像属性设置如下:

        image: {
            url: {
                type: String,
                required: false
            },
            id: {
                type: String,
                required: false
            }
        },

感谢您的帮助,谢谢

访问图像时,您可以尝试:

cloudinary.url("sample.jpg", {width: 300, height: 100, crop: "scale",default_image: "avatar.png"});