提问者:小点点

Post方法值未定义


email2的返回未定义。 方法UserRepository.GetAllConfirmedEmails(。。。) 传递正确的值,但在post方法中返回是未定义的。

  app.post('/login', async (req, res) => {
        const { email } = req.body;
    
        // get safetyAttribues
        const safetyAttributes = await safetyAttributesClass.getSafetyAttributes(email);
    
        // if both are true, try to send login challenge
        if (safetyAttributes.isAccepted && safetyAttributes.isConfirmed) {
            if (safetyAttributes.isAdmin) {
                const email2;
                 const token = await safetyAttributesClass.generateAdminToken(email).then(async (token)  => {
                     email2 = await userRepository.getAllConfirmedEmails(email, token).then(async (email2) => {
                        console.log("HIER STEHT DIE ZWEITE EMAIL-->" , email2);
                     })
                     
                });
                
                
    
            }
            const user = userRepository.findByEmail(email);
            console.log('HIER ISTHEH <USJR', user);
            if (!user) {
                return res.sendStatus(400);
            }
    
            const assertionChallenge = generateLoginChallenge(user.key);
            userRepository.updateUserChallenge(user, assertionChallenge.challenge);
    
            res.send(assertionChallenge);
        }
        // if not, send attributes to frontend to handle client-side
        else {
            res.send({ isAcceptet: safetyAttributes.isAccepted, isConfiremd: safetyAttributes.isConfirmed })
        }
    
    });

共2个答案

匿名用户

您奇怪地将async/await与#混在一起,那么,坚持使用async/await可能会有所帮助:

const token = await safetyAttributesClass.generateAdminToken(email);
const email2 = await userRepository.getAllConfirmedEmails(email, token);
console.log("HIER STEHT DIE ZWEITE EMAIL-->", email2);

匿名用户

您应该在getAllConfirmedEmails回调中返回email2。 像这样

    app.post('/login', async (req, res) => {
            const { email } = req.body;
        
            // get safetyAttribues
            const safetyAttributes = await safetyAttributesClass.getSafetyAttributes(email);
        
            // if both are true, try to send login challenge
            if (safetyAttributes.isAccepted && safetyAttributes.isConfirmed) {
                if (safetyAttributes.isAdmin) {
                    const email2;
                     const token = await safetyAttributesClass.generateAdminToken(email).then(async (token)  => {
                         email2 = await userRepository.getAllConfirmedEmails(email, token)
                         
                    });
                    
                    
        
                }
                const user = userRepository.findByEmail(email);
                console.log('HIER ISTHEH <USJR', user);
                if (!user) {
                    return res.sendStatus(400);
                }
        
                const assertionChallenge = generateLoginChallenge(user.key);
                userRepository.updateUserChallenge(user, assertionChallenge.challenge);
        
                res.send(assertionChallenge);
            }
            // if not, send attributes to frontend to handle client-side
            else {
                res.send({ isAcceptet: safetyAttributes.isAccepted, isConfiremd: safetyAttributes.isConfirmed })
            }
        
        })

;