盒子里的加菲猫 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日