盒子里的加菲猫 2021-12-21 16:41 采纳率: 73.1%
浏览 42
已结题

request里对外部的info赋值不成功!!

问题遇到的现象和发生背景

request里对外部的info赋值不成功!!

问题相关代码,请勿粘贴截图
import {request} from "../network/request/request";

export function CententCheck(mail, title) {
    const info = null;
    request({
        url: 'CententCheck',
        params: {
            mail: mail,
            title: title,
        }
    }).then(res => {
        if (res.data.success) {
            if (res.data.data.length >= 1) {
                this.info = true;
            } else {
                this.info = false;
            }
        } else {
            console.log('数据库错误');
        }
    }).catch(err => {
        console.log('api错误' + err);
    })
    return info;
}
运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-12-21 16:57
    关注

    异步操作需要用回调的形式获取数据,然后需要用到数据的代码放到回调里面。改成下面这种模式

    
        import { request } from "../network/request/request";
        export function CententCheck(mail, title,callback) {
            const info = null;
            request({
                url: 'CententCheck',
                params: {
                    mail: mail,
                    title: title,
                }
            }).then(res => {
                if (res.data.success) {
                    if (res.data.data.length >= 1) {
                        this.info = true;
                    } else {
                        this.info = false;
                    }
    
                    callback(this.info);///
                } else {
                    console.log('数据库错误');
                }
            }).catch(err => {
                console.log('api错误' + err);
            })
            //return info;//没意义,除非request能改为同步请求,否则这里info永远为null
        }
    
    
    
        //调用CententCheck
        CententCheck('mailxxxx', 'titlexxxx', function (info) {
            console.log(info)
        })
    
    

    img


    有帮助或启发麻烦点下【采纳该答案】,谢谢~~有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月10日
  • 已采纳回答 1月2日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么