我无法在nuxt.js mongo express应用中显示单个记录。
以下api 请求在邮递员中返回200响应,并按预期获取ID的记录
// Get Single Report
router.get('/:id', async (req, res) => {
const reports = await loadReportsCollection()
await reports.findOne({_id: new mongodb.ObjectID( req.params.id)})
res.send(await reports.find({}).limit(1).toArray())
res.status(200).send()
}
)
async function loadReportsCollection() {
const client = await mongodb.MongoClient.connect('mongodb://localhost:27017/reports/', {
useNewUrlParser: true
})
return client.db('monthlyreport').collection('reports')
}
页面/报告/index.vue中单个记录的链接是
<NuxtLink :to="{ name: 'reports-id', params: { id: report._id } }">
{{ report.month }}
</NuxtLink>
和页面/报告/_id.vue中前端上的请求在下面,但不返回记录-错误是
“获取http://localhost:5000/api/reports/undefined net :: ERR_EMPTY_RESPONSE“
<script>
import axios from 'axios'
export default {
data() {
return {
report: {}
}
},
async asyncData({ params }) {
const { data } = await axios.get(
`http://localhost:5000/api/reports/${params._id}`
)
return { report: data }
}
}
</script>
为什么未定义ID?