내가 Mongoose에서 JSON 속성에 액세스 할 수없는 이유는 무엇입니까? 내 경로에
router.get('/users/:user', function(req, res) {
mongoose.model('users').find({name: req.params.user}, function(err, user) {
res.send(user.name || 'User.name is not defined');
});
});
이 그리고 내 스키마가 난 그냥
res.send(user)
을 할 경우, 그것은 모두와 함께 다시 나에게 적절한 JSON을 제공합니다, 내 루트 파일에서
var userDataSchema = new Schema({
name: String
}, {collection: 'users'});
mongoose.model('users', userDataSchema);
으로 정의된다, 파일 그들의 이름을 가진 사용자. 내 문제는 내가 JSON에서 특정 이름에 액세스 할 수 없다는 것입니다.
나는 그 변수에 JSON.stringify(user)
을 시도한 다음 JSON.parse()
을 시도했지만 user.name은 여전히 액세스 할 수 없습니다. 나는 이제 user.toJSON()
을 시도했다, 그러나 이것은 오류가 발생하고 심지어
편집이 '정의되지 않은 User.name'로 응답하지 않습니다 다음은 JSON입니다 : 나는에 갈 때 그래서
[
{
_id: "57feb97017910fa7e0e194d8",
name: "Garrett"
},
{
_id: "57feb97817910fa7e0e194d9",
name: "Steve"
},
{
_id: "57feb97c17910fa7e0e194da",
name: "Joe"
}
]
/사용자/Steve와 console.log(user)
의 경우 해당 사용자에 대한 올바른 JSON을 기록합니다.
것이다. 로깅이란 무엇입니까? – user2263572
그것은 정의되지 않은 –
을 기록합니다. [toJSON] (http://mongoosejs.com/docs/api.html#document_Document-toJSON) 메서드를 호출해야한다고 생각합니다. – tmquang6805