Is it good practice to do POST within GET request?
P粉434996845
P粉434996845 2023-09-12 09:11:31
0
1
569

As the title says, I want to make a POST request at the same time as a GET request. Is this good practice? Or a huge no-no?

This is because my app functionality works as follows: when the page loads, it needs to get the user data. If the user data is not in the database, it should be added to the database. unregistered. We are tracking all users on the page. (The number of times they viewed the site and their ID)

EDIT: We do not track user login information. Or sign them up. We will record the number of times they visit the website in our database.

I tried setting it up using GET and then onSuccess running the POST request, but that seems like overkill.

export default function handler(req, res) {
  if (req.method === 'GET') {
    // check db if user is there (db.get())
    // if not run the PUT request. (db.save())
  } 
}

P粉434996845
P粉434996845

reply all(1)
P粉244155277

Maybe there is a better way. For example, if you use DynamoDB, you can update items using . This allows you to update the project if it exists, or publish a new project if it does not exist. Additionally, by using the ReturnValues parameter, the operation will return any properties you want. You can get everything you want with one of these calls.

But if not, this looks good to me. If you want to avoid nested callbacks, you can separate calls using the async and await syntax:

export default function handler(req, res) {
  if (req === "GET") {
    const result = await getRequest();
    if (result.isEmpty()) {
      postRequest();
    }
  }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template