我正在尝试使用 Vue 和 FaunaDB 构建一个简单的网络应用程序。尝试从数据库中获取数据时,出现以下错误:
localhost/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
当我打印来自 Netlify 功能服务器的响应时,我得到的是:
这是尝试获取数据的 vue 页面中的代码:
created() {
EventService.readAll()
.then(response => {
this.events = response.data
})
}
这是 EventService 模块:
const readAllDates = () => {
console.log("hey")
return fetch('/.netlify/functions/read-all-dates').then((response) => {
console.log(response)
return response.json()
})
}
export default {
readAll: readAllDates
}
这是我的 read-all-dates.js:
import faunadb from 'faunadb'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
})
exports.handler = (event, context, callback) => {
console.log("Function `read-all-dates` invoked")
return client.query(q.Paginate(q.Match(q.Ref("indexes/all_dates"))))
.then((response) => {
const dateRefs = response.data
console.log("Todo refs", dateRefs)
console.log(`${dateRefs.length} todos found`)
const getAllDateDataQuery = dateRefs.map((ref) => {
return q.Get(ref)
})
// then query the refs
return client.query(getAllDateDataQuery).then((ret) => {
return callback(null, {
statusCode: 200,
body: JSON.stringify(ret)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}
我究竟做错了什么?