无法从NestJS应用程序中的Redis获取数据



我在redis中存储了密钥,我正试图在嵌套应用程序中获取值,但控制台中显示错误:

SyntaxError: Unexpected token d in JSON at position 0

在postmna中,它将响应显示为空json对象,就像{ }一样,如果redis 中有密钥,为什么会显示空对象

以下是我的代码:

电话.dto.ts

import { IsNotEmpty, MinLength } from "class-validator";
export class PhoneNumber 
{
@IsNotEmpty({message: 'Phone number cannot be empty'})
@MinLength(10, {message: 'Invalid phone number'}) 
phoneNumber:string;
}

订阅控制器.ts

@Post('location')
async getLocation(@Body(ValidationPipe) phoneNumber:PhoneNumber){
const loc = await this.subscribeService.getLocation(phoneNumber);
return loc; 
}

subscribe.service.ts'

async getLocation(phoneNumber:PhoneNumber){

try{
console.log(phoneNumber.phoneNumber);

const valu = await this.cacheManager.get(phoneNumber.phoneNumber);

if(valu){
return valu; 
}
else{
return 'No key found';
}
}
catch(err){
console.log(err); 
return err;
}
}

有人告诉我为什么我会遇到这个问题。

您可以添加数据作为密钥:

async getLocation(phoneNumber:PhoneNumber){   
try{
console.log(phoneNumber.phoneNumber);

const valu = await this.cacheManager.get(phoneNumber.phoneNumber);

if(valu){
return {data:valu}; //like this
}
else{
return 'No key found';
}
}
catch(err){
console.log(err); 
return err;
}
}

plz投赞成票,如果有用的话:(

最新更新