Uploading multiple images using code

Hi Guys

For a number of different reasons, I need to upload images along with some other items using code. I’ve written the script below, but I just can’t make it work. If one uploads 5 images and the input data, it only captures 4 images. If one uploads 3 images and some data, it only captures 2 images, and so on. Any advice on how to make it work…?

export function button8_click(event, $w) {

    $w("#dataset3").setFieldValue("title", $w("#input11").value);
    $w("#dataset3").setFieldValue("propertyType", $w("#input12").value);
    $w("#dataset3").setFieldValue("beds", $w("#input13").value);
    $w("#dataset3").setFieldValue("location", $w("#input14").value);
    $w("#dataset3").setFieldValue("address", $w("#input15").value);
    $w("#dataset3").setFieldValue("postcode", $w("#input16").value);
    $w("#dataset3").setFieldValue("price", $w("#input17").value);
    $w("#dataset3").setFieldValue("priceNumberOnly", $w("#input18").value);
    $w("#dataset3").setFieldValue("yield", $w("#input19").value);
    $w("#dataset3").setFieldValue("url", $w("#input20").value);
    $w("#dataset3").setFieldValue("agentEmail", $w("#textAgentEmail").text);
    $w("#dataset3").setFieldValue("views", "0");
    $w("#dataset3").setFieldValue("emails", "0");

 if ($w("#uploadButton3").value.length > 0) {
        $w("#uploadButton3").startUpload()
            .then((uploadedFile) => {
                $w("#dataset3").setFieldValue("image1", uploadedFile.url)
            })
            .then(() => {
                if ($w("#uploadButton4").value.length > 0) {
                    $w("#uploadButton4").startUpload()
                        .then((uploadedFile) => {
                            $w("#dataset3").setFieldValue("image2", uploadedFile.url)
                        })
                        .then(() => {
                            if ($w("#uploadButton5").value.length > 0) {
                                $w("#uploadButton5").startUpload()
                                    .then((uploadedFile) => {
                                        $w("#dataset3").setFieldValue("image3", uploadedFile.url)
                                    })
                                    .then(() => {
                                        if ($w("#uploadButton6").value.length > 0) {
                                            $w("#uploadButton6").startUpload()
                                                .then((uploadedFile) => {
                                                    $w("#dataset3").setFieldValue("image4", uploadedFile.url)
                                                })
                                                .then(() => {
                                                    if ($w("#uploadButton7").value.length > 0) {
                                                        $w("#uploadButton7").startUpload()
                                                            .then((uploadedFile) => {
                                                                $w("#dataset3").setFieldValue("image5", uploadedFile.url)
                                                            })
                                                            .then(() => {
                                                                if ($w("#uploadButton8").value.length > 0) {
                                                                    $w("#uploadButton8").startUpload()
                                                                        .then((uploadedFile) => {
                                                                            $w("#dataset3").setFieldValue("image6", uploadedFile.url)
                                                                        });
                                                                }
                                                            });
                                                    }
                                                });
                                        }
                                    });
                            }
                        });
                }
            })
            .then(() => {
                $w("#dataset3").save(); 
            });
    }
}

Thanks
Tiaan

Hey
You have the different .then inside each other…

.then((uploadedFile) => {                 

$w("#dataset3").setFieldValue("image1", uploadedFile.url) 

if ($w("#uploadButton4").value.length > 0) {                    
$w("#uploadButton4").startUpload() .then((uploadedFile) => {

})

So they are inside each other during the process, when the first is done, start the second code, do not end the first one with }); and try making another .then outside, won’t do it. Make the code start inside the first then and then keep on doing that.

Hi Andreas

Thanks for the advice, that might do it!

Tiaan