大佬教程收集整理的这篇文章主要介绍了Uncaught (in promise) SyntaxError: Unexpected end of JSON input on my console,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的控制台返回:Uncaught (in promisE) SyntaxError: Unexpected end of JsON input
,我的终端返回:{ categoryID: undefined }
这是我的路线:
router.get('/getAll',auth.verify,(req,res) => {
const user = auth.decode(req.headers.authorization);
const categoryID = req.params.categoryID
UserController.getCat({ categoryID })
.then(category => res.send(category))
})
这是我的控制器:
@H_9_3@module.exports.getCat = (params) => { console.log(params) return User.findByID(params.categoryID) .then(resultFromFindByID => resultFromFindByID) }
我想在我的组件上使用此 fetch 获取所有数据,请帮助我确定问题所在.. 似乎我的语法没有问题
useEffect(() => {
fetch(`${process.env.NEXT_PUBliC_API_URL}/users/getAll`,{
headers: {
'Authorization': `Bearer ${localstorage.getItem('token')}`
},})
.then(res => res.Json())
.then(data => {
if(data) {
console.log(data)
}
})
},[])
你定义了req.params.categoryId,顺便说一下它是字符串。因此,当您执行函数 getCat 并将其定义为对象时,它返回错误。
router.get('/getAll',auth.verify,(req,res) => {
const user = auth.decode(req.headers.authorization);
const categoryId = req.params.categoryId // this categoryId is String
UserController.getCat({ categoryId }) // you define it inside curly bracket and hope it treats as object,so this causes error
.then(category => res.send(category))
})
将 categoryId 定义为对象并使用原始 json 主体的更好方法,它看起来像这样:
router.get('/getAll',res) => {
const user = auth.decode(req.headers.authorization);
// this param variable means object and use it in function getCat
const param = {
categoryId: req.body.categoryId
}
UserController.getCat(param)
.then(category => res.send(category))
})
以上是大佬教程为你收集整理的Uncaught (in promise) SyntaxError: Unexpected end of JSON input on my console全部内容,希望文章能够帮你解决Uncaught (in promise) SyntaxError: Unexpected end of JSON input on my console所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。