0
다음 코드는 제대로 작동하지 않습니다. 내 개체의 일부 하위 문서가 정의되어 있으면 내 몽구수 findOne 쿼리를 확인하고 싶습니다.몽구스 개체에 하위 필드가 있는지 확인하는 기능
function cleanIfAny(social, value) {
var name = social + '.id';
var query = {};
query[name] = value;
User.findOne(query, function (err, found) {
if (err) console.log(err);
// if the account is already linked somewhere else
if (found) {
// detach it from there
found[social] = undefined;
// TODO Check if field in document is present
console.log(found.local + ' ' + found.google + ' ' + found.facebook);
console.log(isEmpty(found.local)+ ' ' + isEmpty(found.google) + ' ' + isEmpty(found.facebook));
if(isEmpty(found.local) && isEmpty(found.google) && isEmpty(found.facebook)) {
console.log('no more account attached deleting');
found.remove();
} else {
console.log('ok unlink the account');
found.save(function (err) {
if (err) console.log(err);
})
}
}
})
}
function isEmpty(obj) {
return (obj == null || Object.keys(obj).length === 0);
}
이 코드 리턴 나의 IsEmpty 함수 (예상대로 대신 그러나 나는 몇 가지 간단한 변수와 모든 작업에이 범위 이외의 제 기능을 시도이
true false true
의
{} { token: 'abcdefgh, name: 'FirstName LastName', id: '123456789', email: '[email protected]' } undefined
false false false
ok unlink the account
) 기능. 바퀴에게 모든 시간을 개혁하면 저장
https://lodash.com/docs#isEmpty
:
나는 그냥 lodash를 사용하여 결과를 내 함수 내에서 동일하게 만들었습니다. 아마도 mongoose에 의한 형식 반환은 내가 예상 한 것이거나 전체 기능에서 나쁘다. – melkir
필자의 경우 typeof found, found.local, found.google 및 found.facebook은 정의되지 않은 객체 일 수도있다. – melkir
mongoose 문서에는 실제 mongo 객체가 저장되는'_doc' 속성이있다. 열쇠는 최고 수준이 아닙니다. 나는'doc.property === undefined'를 검사하는 것이 효과가 있다고 생각한다. – cdbajorin