I am working on a small project in which data after filling form will be posted on notion.
Error while submitting button from form:contact.jsx?287f:14 POST http://localhost:3000/contact 405 (Method Not Allowed)enter link description here
sorce:-
notion db
here were ui renders
contact.jsx::-
”’
import { useState } from 'react';
import ContactCode from '../components/ContactCode';
import styles from '../styles/ContactPage.module.css';
const ContactPage = () => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [subject, setSubject] = useState('');
const [message, setMessage] = useState('');
const submitForm = async (e) => {
e.preventDefault();
console.log(process.env.NEXT_PUBLIC_API_URL);
const res = await fetch(`http://localhost:3000/contact`, {
method: 'post',
body: JSON.stringify({
name: name,
email: email,
subject:subject,
message: message
}),
});
if (res.ok) {
alert('Your response has been received!');
setName('');
setEmail('');
setSubject('');
setMessage('');
} else {
alert('There was an error. Please try again in a while.');
}
};
return (
<div className={styles.container}>
<div>
<h3 className={styles.heading}>Reach Out Via Socials</h3>
<ContactCode />
</div>
<div>
<h3 className={styles.heading}>Or Fill Out This Form</h3>
<form className={styles.form} onSubmit={submitForm}>
<div className={styles.flex}>
<div>
<label htmlFor="name">Name</label>
<input
type="text"
name="name"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
required
/>
</div>
<div>
<label htmlFor="email">Email</label>
<input
type="email"
name="email"
id="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
</div>
<div>
<label htmlFor="name">Subject</label>
<input
type="text"
name="subject"
id="subject"
value={subject}
onChange={(e) => setSubject(e.target.value)}
required
/>
</div>
<div>
<label htmlFor="message">Message</label>
<textarea
name="message"
id="message"
rows="5"
value={message}
onChange={(e) => setMessage(e.target.value)}
required
></textarea>
</div>
<button type="submit">Submit</button>
</form>
</div>
</div>
);
};
export async function getStaticProps() {
return {
props: { title: 'Contact' },
};
}
export default ContactPage;
”’
api/contact.js :-
const { Client } = require('@notionhq/client');
const notion = new Client({
auth: 'secret_xxxxxxxxxxxxxxxxxxxxx',
});
export default async (req, res) => {
if (req.method !== 'POST') {
return res.status(405).json({ msg: 'Only POST requests are allowed' });
}
try {
const { name, email, subject, message } = JSON.parse(req.body);
await notion.pages.create({
parent: {
database_id: 'https://www.notion.so/62xxxxx713xx`,
},
properties: {
Name: {
title: [
{
text: {
content: name,
},
},
],
},
Email: {
email,
},
Subject: {
rich_text: [
{
text: {
content: subject,
},
},
],
},
Message: {
rich_text: [
{
text: {
content: message,
},
},
],
},
},
});
res.status(201).json({ msg: 'Success' });
} catch (error) {
console.log(error);
res.status(500).json({ msg: 'Failed' });
}
};