在另一个静态方法(JavaScript)中调用静态方法时,“ this”关键字未定义

时间:2019-08-19 08:43:04

标签: javascript

我正在导出/导入一个空数组以实现MVC模式,通过调用 Mentor 类上的 allMentors 方法,我能够在其中成功存储数据。

第二,我想进行干燥并从数组中获取数据,然后使用它来查找特定的指导者,但是我得到的是一个空数组。

我在互联网上搜索,并且遵循了使用此关键字的示例,并在另一个示例中调用static方法,但是NodeJS抛出了一个错误,该错误是未定义的。

所有指导者方法

class Mentor{

static async allMentors(req, res) {

        try {

        users.forEach(user => {
            if(user.is_mentor === true) {
                mentors.push(user);
            }
        })

        const ObjKeyRename = (src, map) => {
                const dst = {};

                for (const key in src) {
                    if (key in map)
                        // rename key
                        dst[map[key]] = src[key];
                    else
                        // same key
                        dst[key] = src[key];
                }
                return dst;
        };

        const uniqueMentors = Array.from(new Set(mentors.map(m => m.id)))
                .map(id => {
                   return  new Promise((resolve, reject)=> {
                        const currMentor =  mentors.find(m => m.id === id);
                        const modMentor =  ObjKeyRename(currMentor, { "id": "mentorId" });
                        return resolve(modMentor);
                    })
                }) 

        Promise.all(uniqueMentors).then(output => {
            output.forEach(async obj => {
               await delete obj['password'];
            })

            return res
            .status(200)
            .json(new ResponseHandler(200, 'All Mentors', output, null).result());
        })

        } catch (err) {
            return res
                .status(500)
                .json(new ResponseHandler(500, err.message, null).result());

        }

    }


static async singleMentor(req, res) {

        const returnedMentors = this.allMentors; 
        console.log('THE RETURNED:',returnedMentors)
        const theMentor = returnedMentors.find(u => u.mentorId === parseInt(req.params.mentorId));

        try {
            if (!theMentor) return res
                .status(404)
                .json(new ResponseHandler(404, `Mentor number ${req.params.mentorId} not found`, null).result());

            return res
                .status(200)
                .json(new ResponseHandler(200, 'Your mentor', theMentor, null).result());

        } catch (err) {
            return res
                .status(500)
                .json(new ResponseHandler(500, err.message, null).result())
        }

    }
}

export default Mentor;

我在做什么错?感谢您为JS学习者提供的持续帮助。

1 个答案:

答案 0 :(得分:0)

您的代码有多个问题:

  • 需要通过类(Mentor)而不是引用(this)调用静态方法
  • 异步方法需要等待或通过回调(.then())调用

例如,您有一个MentorHandler类:

class Mentor {
  constructor() {
  }

  static async singleMentor(req, res) {

    // ... some more code
  }

  static async allMentors(req, res) {
    // await the result of the method
    // call by class
    const returnedMentors = await Mentor.allMentors(); 


    // ... some more code
  }
}