2017-09-24 10 views
1

normalizr을 사용하여 다음 데이터를 표준화하려고합니다. 위치 개체를 쉽게 추출 할 수 있어야합니다. 더 구체적으로 위치 객체 내에 중첩 된 movie 객체를 갖기를 원합니다.은 normalizr을 사용하여 데이터를 정규화합니다.

내 코드 : 아약스 호출 (results.data)에서 데이터를 검색 한 후 평면화하려고합니다.

const location = await new schema.Entity(
    'locations', 
    {}, 
    { 
    idAttribute: '_id' 
    } 
); 
const movie = await new schema.Entity(
    'movies', 
    { locations: [location] }, 
    { 
    idAttribute: '_id' 
    } 
); 

const normalizedData1 = normalize(result.data, movie); 

내가 노력하고 데이터는 정상화 :

[ 
    { 
    _id: '59c7698d544d56b262e2187e', 
    title: 'tomorrow never die', 
    __v: 0, 
    locations: [ 
     { 
     address: 'lasselle strasse 12 39114 magdeburg', 
     lng: '11.6571759', 
     lat: '52.1229847', 
     _id: '59c7698d544d56b262e21880' 
     }, 
     { 
     address: ' hindhede place 12 587852 singapore', 
     lng: '103.7760416', 
     lat: '1.3454118', 
     _id: '59c7698d544d56b262e2187f' 
     } 
    ] 
    }, 
    { 
    _id: '59c76a87544d56b262e21881', 
    title: 'today is the day', 
    __v: 0, 
    locations: [ 
     { 
     address: '1 high street flemington 3031', 
     lng: '144.933632', 
     lat: '-37.784413', 
     _id: '59c76a87544d56b262e21883' 
     }, 
     { 
     address: ' 18 rue benoit malon', 
     lng: '2.3515209', 
     lat: '48.8071822', 
     _id: '59c76a87544d56b262e21882' 
     } 
    ] 
    }, 
    { 
    _id: '59c76aeca1429ab37e9d40bd', 
    title: '3 days to go', 
    __v: 0, 
    locations: [ 
     { 
     address: '35 Nasse Wenne Paderborn', 
     lng: '8.7262077', 
     lat: '51.73028189999999', 
     _id: '59c76aeca1429ab37e9d40be' 
     } 
    ] 
    }, 
    { 
    _id: '59c76b6788e12ab3a6d90fea', 
    title: 'X men', 
    __v: 0, 
    locations: [ 
     { 
     address: '18 rue benoit malon sevres france', 
     lng: '2.2042506', 
     lat: '48.817549', 
     _id: '59c76b6788e12ab3a6d90feb' 
     } 
    ] 
    } 
]; 

전류 출력 : result.data 이후

{ 
     "entities": { 
     "movies": { 
      "undefined": { 
      "0": { 
       "_id": "59c7698d544d56b262e2187e", 
       "title": "tomorrow never die", 
       "__v": 0, 
       "locations": [ 
       { 
        "address": "lasselle strasse 12 39114 magdeburg", 
        "lng": "11.6571759", 
        "lat": "52.1229847", 
        "_id": "59c7698d544d56b262e21880" 
       }, 
       { 
        "address": " hindhede place 12 587852 singapore", 
        "lng": "103.7760416", 
        "lat": "1.3454118", 
        "_id": "59c7698d544d56b262e2187f" 
       } 
       ] 
      }, 
      "1": { 
       "_id": "59c76a87544d56b262e21881", 
       "title": "today is the day", 
       "__v": 0, 
       "locations": [ 
       { 
        "address": "1 high street flemington 3031", 
        "lng": "144.933632", 
        "lat": "-37.784413", 
        "_id": "59c76a87544d56b262e21883" 
       }, 
       { 
        "address": " 18 rue benoit malon", 
        "lng": "2.3515209", 
        "lat": "48.8071822", 
        "_id": "59c76a87544d56b262e21882" 
       } 
       ] 
      }, 
      "2": { 
       "_id": "59c76aeca1429ab37e9d40bd", 
       "title": "3 days to go", 
       "__v": 0, 
       "locations": [ 
       { 
        "address": "35 Nasse Wenne Paderborn", 
        "lng": "8.7262077", 
        "lat": "51.73028189999999", 
        "_id": "59c76aeca1429ab37e9d40be" 
       } 
       ] 
      }, 
      "3": { 
       "_id": "59c76b6788e12ab3a6d90fea", 
       "title": "X men", 
       "__v": 0, 
       "locations": [ 
       { 
        "address": "18 rue benoit malon sevres france", 
        "lng": "2.2042506", 
        "lat": "48.817549", 
        "_id": "59c76b6788e12ab3a6d90feb" 
       } 
       ] 
      } 
      } 
     } 
     } 
    } 

답변

-1

이 내가

const location = await new schema.Entity(
    'locations', 
    {}, 
    { 
    idAttribute: '_id' 
    } 
); 
const movie = await new schema.Entity(
    'movies', 
    { locations: [location] }, 
    { 
    idAttribute: '_id' 
    } 
); 

const movieListSchema = [movie]; 

const normalizedData = normalize(result.data, movieListSchema); 
1

배열, 당신은 당신이에 대해 정상화하고 스키마를 만들 필요가있다 배열뿐만 아니라. 그렇게하는 가장 간단한 방법은 normalize에 대한 호출에 있습니다

const normalizedData1 = normalize(result.data, [ movie ]); 
+0

예에 결국 무엇인가, 그게 내가 너무 가지고거야. 감사. –