私は次のサイトを構築しており、Fauna をデータベースとして使用することにしました。フロントエンドでは、次のようにオブジェクトをバックエンド API に渡しています。
async function onSubmit(values) {
try {
const data = await postData("/api/put", values);
} catch (error) {
console.error(error);
}
async function postData(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
referrer: "no-referrer",
body: JSON.stringify(data)
});
return await response.json(); // parses JSON response into native JavaScript object
}
}
私のバックエンドは次のようになります。
const faunadb = require("faunadb");
// your secret hash
//! -- Replace with secret for prod.
const secret = "Key...";
const q = faunadb.query;
const client = new faunadb.Client({ secret });
module.exports = async (req, res) => {
const data = JSON.stringify(req.body, null, 2);
console.log(data);
return client
.query(
q.Create(q.Collection("pages"), {
data: {
name: data.name,
email: data.email,
title: data.title,
body: data.body
}
})
)
.then(ret => console.log(ret))
.catch(err => console.log("error", err));
};
返品されたようで、data
認識されず、Fauna コレクションに返送されたアイテムは空です。誰かが私が間違っていることを教えてもらえますか?
Fauna Docsには、変数を使用した例は示されていません。