2017-06-11 80 views
0

我正在使用猫鼬从数据库中获取人员数据。这是我使用的代码:Mongoose模型对象行为奇怪

return new Promise((resolve, reject) => { 
    Person.findOne({}, (err, result) => { 
     if(err) { 
     reject(err); 
     } else { 
     console.log(result); 
     console.log(result.firstname); 
     console.log(result.githubLink); 
     resolve(result); 
     } 
    }); 
    }); 

这是来自的console.log(结果)输出

{ _id: 593c35e6ed9581db3ef85d75, 
firstname: 'MyName', 
lastname: 'MyLastName', 
jobtitle: 'Web Developer', 
email: '[email protected]', 
githubLink: 'https://github.com/myGithub' } 

这是从的console.log(result.firstname)结果;和console.log(result.githubLink);

MyName 
undefined 

这个承诺是不知何故搞乱了这个结果?这真的很奇怪,因为只记录结果显示我的github链接并记录链接说未定义。

+0

是你确定在任何地方都没有任何拼写错误? –

+0

我已经从console.log(result)的输出复制并粘贴了“githubLink”,并且我一直在盯着这段代码30分钟。我不认为这是一个错字 – Frostbch

+1

复制你正在做日志的代码的实际行,并在你的问题中显示模式。 –

回答

2

如果数据库对象中存在的字段实际上不存在于为该模型定义的模式中,那么它们仍会“记录”,但无法正常访问该属性的值。

在大多数情况下,你真的想在你的架构正确定义项:

githubLink: String 

或者你可以访问属性,你故意不想使用.get()方法来定义:

result.get('githubLink')