我正在尝试设置 SvelteKit 以将其与我的 CMS 一起使用。
我正在从我的 CMS in 中api/query.js
获取数据,并从api/query
in 中获取数据index.svelte
。
它工作得很好,我可以获取整个数据,但如果我在获取请求中包含正文,我会收到错误“无法读取未定义的属性‘拆分’”。代码:
// api/query.js
export async function post(req) {
const url = API_URL;
const auth = Buffer.from(`${API_USER_EMAIL}:${API_USER_PASSWORD}`).toString('base64');
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Basic ${auth}`,
},
body: JSON.stringify(req.body),
});
const data = await res.json();
return {
status: data.code,
body: data
}
}
// index.svelte
export async function load({ fetch }) {
const res = await fetch('/api/query', {
method: 'POST',
// body: JSON.stringify({
// query: 'site.title',
// }),
});
const data = await res.json();
return {
status: data.status || 200,
props: data
}
}
代码中被注释掉的部分是导致错误的部分。如果我console.log(req.body)
在api/query.js
它返回undefined
。
有没有办法可以使用 Express.js 正文解析器?或者有没有其他方法可以解决这个错误?