如何从NeDB查找查询中返回值而不是未定义

时间:2019-04-04 13:08:45

标签: javascript node.js ejs nedb

我想从电子邮件中大写名字(电子邮件格式为name.lastname@company.com)。

为此,我传递了id参数来获取电子邮件,然后调用一个函数来使名字大写。

routes.get('/mail/:id', (req, res)=>{
     var id = req.params.id;
     request({
         url: '....,
         json: true
       }, function(error, response, body) {
         if (!error && response.statusCode === 200) {
             res.render("mail.ejs", {username: getName(id)});

        }....        
 });
});


function getName(id){
    db.find({ _id: id }, function (err, docs) {
        if (err) throw err;
        return capitalizeName((docs[0].email.split(".")[0]));        
    });   

}

function capitalizeName(string) {    
    return string.charAt(0).toUpperCase() + string.slice(1);
}

假设我的电子邮件是john.sander@company.com。 因此,我希望收到John并将其传递来呈现mail.ejs,但是我收到undefined

0 个答案:

没有答案