点赞和取消点赞的接口报错,一直报错Cannot read property 'getData' of undefined,找不到解决方案
来源:10-1 获取期刊点赞信息

wn798793618
2021-01-07
/*
* favor.js
*/
const {
Art
} = require('@models/art');
const {
Sequelize,
Model
} = require('sequelize');
const {
sequelize
} = require('@core/db');
const {
LikeError,
DisLikeError
} = require('@core/http-exception')
class Favor extends Model {
static async like(art_id, type, uid) {
const favor = await Favor.findOne({
where: {
art_id,
type,
uid
}
})
if (favor) {
throw new LikeError();
}
return sequelize.transaction(async t => {
await Favor.create({
art_id,
type,
uid
}, {
transaction: t
})
console.log('%c ? Art: ', 'font-size:20px;background-color: #FFDD4D;color:#fff;', Art);
const art = await Art.getData(art_id, type, false);
await art.increment('fav_nums', {
by: 1,
transaction: t
})
})
}
static async disLike(art_id, type, uid) {
const favor = await Favor.findOne({
where: {
art_id,
type,
uid
}
})
if (!favor) {
throw new DisLikeError();
}
return sequelize.transaction(async t => {
await favor.destroy({
force: true,
transaction: t
})
const art = await Art.getData(art_id, type, false);
await art.decrement('fav_nums', {
by: 1,
transaction: t
})
})
}
static async userLikeIt(art_id, type, uid) {
const favor = await Favor.findOne({
where: {
uid,
art_id,
type
}
})
return favor ? true : false
}
}
// 业务表
Favor.init({
uid: Sequelize.INTEGER,
art_id: Sequelize.INTEGER,
type: Sequelize.INTEGER
}, {
sequelize,
tableName: 'favor'
})
module.exports = {
Favor
}
/*
* like.js
*/
const Router = require('koa-router');
const router = new Router({
prefix: '/v1/like'
});
const {
Favor
} = require('@models/favor');
const {
Auth
} = require('@middleware/auth');
const {
LikeValidator,
} = require('@validator')
const {
Success
} = require('@core/http-exception')
router.post('/', new Auth().m, async (ctx, next) => {
const v = await new LikeValidator().validate(ctx, {
id: 'art_id'
});
const art_id = v.get('body.art_id');
const type = v.get('body.type');
const uid = ctx.auth.uid;
await Favor.like(art_id, type, uid)
throw new Success();
})
router.post('/dislike', new Auth().m, async (ctx, next) => {
const v = await new LikeValidator().validate(ctx, {
id: 'art_id'
});
const art_id = v.get('body.art_id');
const type = v.get('body.type');
const uid = ctx.auth.uid;
await Favor.disLike(art_id, type, uid)
throw new Success();
})
module.exports = router;
然后接口调用时报错
TypeError: Cannot read property 'getData' of undefined
写回答
1回答
-
7七月
2021-01-07
这真不需要什么解决方案,就是代码写错了,调试下吧。学会断点调试,或者console打印看看
00
相似问题
点赞点击报错
回答 3
调用api一直是404(已解决)
回答 3