Skip to content

Commit

Permalink
Merge branch 'canary' into firebase-auth-example-token-expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
joejknowles authored Aug 5, 2020
2 parents 163b181 + 669cad9 commit c7b434c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion examples/with-mongodb-mongoose/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
},
body: JSON.stringify(form),
})

// Throw error with status code in case Fetch API req failed
if (!res.ok) {
throw new Error(res.status)
}

const { data } = await res.json()

mutate(`/api/pets/${id}`, data, false) // Update the local data without a revalidation
Expand All @@ -45,14 +51,20 @@ const Form = ({ formId, petForm, forNewPet = true }) => {
/* The POST method adds a new entry in the mongodb database. */
const postData = async (form) => {
try {
await fetch('/api/pets', {
const res = await fetch('/api/pets', {
method: 'POST',
headers: {
Accept: contentType,
'Content-Type': contentType,
},
body: JSON.stringify(form),
})

// Throw error with status code in case Fetch API req failed
if (!res.ok) {
throw new Error(res.status)
}

router.push('/')
} catch (error) {
setMessage('Failed to add pet')
Expand Down

0 comments on commit c7b434c

Please sign in to comment.