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

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

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


    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


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

异步请求,请求要时间的嘛,你还没请求完就开始返回了肯定得不到结果啊。
可以学习一下我的博客了解一下异步的解决方案。
js Promise与async/await用法详解:https://blog.csdn.net/weixin_43877799/article/details/121536943