fetch http request err 500

Hi friends,
getting err 500 for http fetch request.
url is:
https://rambamdonations.com/_functions/updateDonation/f20f0a7a-2982-4f27-b5bd-f364a99285e/d7feeaae-a06e-427d-ba1e-7da676c49d7/30/12


Will appreciate some HELP here very very much!!!
Thansks,
Dafna

You are running an wixData.update inside the return funtion of the wixData.query, I think you will need to make the update end with .then() you know and put the code that should run after the update inside that. If you d onot nest them correctly errors might occur. Another solution would be to use async and await and break everything out to smaller functions. Just make sure you console.log(”I am here now”) and you might be able to track down wher ethe error occurs. Also try to use the catch to see where and what triggers the error.

I cant see all the code just the top parts

Thanks.
It didn’t work with a small function as well…

Hey,
After finishing with the updating the collection, you should return the relevant value. It seems like you’ve returned the promise but inside the “then” part you haven’t returned anything.
If it’s unclear, paste here the rest of the code and I’ll try to explain it better with your code.
Moreover, as Andreas mentioned, I recommend splitting the code to smaller functions. It will help you with debugging your code.

Good luck,
Tal.

Getting err 500 even for a shorten function:

What does it say in the developer console? If you console.log(request.path[1]) and also console.log(results.items[0])

don’t know if & how to get the log in preview mode when fetching http request.
also didn’t manage too work with postman

Have you tried returning the toUpdate variable after updating the relevant collection? (line 30). Moreover, I recommend using Postman chrome extension to check the get and post requests.

Thanks Tal!
I triedmto return, it didn’t help.
Working with Postman is not clear to me…
This is what I get, again err 500:

Hi,
When using HTTP requests, you should return a response and not a JSON object. For that purpose, you can use the OK response as explained here . Make sure to set the header response and an informative message so that you know if the object was updated or not:

import wixData from 'wix-data';
import {ok} from 'wix-http-functions';

export async function get_updateDonation(request) {
 //query a collection to find matching items
 //=========================================
 let options = {
 "headers": {
 "Content-Type": "application/json"
        }
    };
 // return ok(options);
 if (request.path.length === 0) {
        options.body = {
            found: false,
            message: 'No parameter given'
        }
 return ok(options);
    }

 const results = await wixData.query("Donations")
        .eq("_id", request.path[0])
        .find();

 if (results.items.length > 0) {
 //update donation
 //===============
 let toUpdate = {
 "_id": request.path[0],
 "name": results.items[0].name,
 "phone": results.items[0].phone,
 "email": results.items[0].email,
 "country": results.items[0].country,
 "address": results.items[0].address,
 "swimmer": request.path[2],
 "amount": request.path[4]
        };
 try {
 let updated = await wixData.update("Donations", toUpdate);
            options.body = {
                updateResult: updated
            }
        } catch(err) {
            options.body = {
                updateResult: err
            }
        }
 return ok(options);
    }
    options.body = {
        found: false,
        message: 'Item was not found'
    }
 return ok(options);
}

I hope it’s clearer now.

Best,
Tal.

Thank you so much!