다음은 JSON 객체에서 가져 오기를 수행하는 방법입니다.
관계에 따라 올바른 순서로 데이터를 가져 오는 것을 숙지해야합니다. 예를 들어, 회사가 아직 존재하지 않기 때문에 먼저 직원을 가져 와서 회사를 할당 할 수 없습니다. 올바른 주문은 회사를 가져온 다음 직원을 가져 와서 회사를 연결하는 것입니다.
/**
* Example Data
*/
var hireDate = new Date('2012',7,13,1,0,0),
companies = [{name:'Company 1'},{name:'Company 2'},{name:'Company 3'}],
employees = [{firstName:'John',lastName:'Doe',company:'Company 1',hireDate:hireDate,salary:2000},
{firstName:'Frederic',lastName:'Smith',company:'Company 2',hireDate:hireDate,salary:2000},
{firstName:'John',lastName:'Doe',company:'Company 3',hireDate:hireDate,salary:3000},
{firstName:'Marc',lastName:'Petit',company:'Company 2',hireDate:hireDate,salary:5000},
{firstName:'Arthur',lastName:'Johns',company:'Company 3',hireDate:hireDate,salary:2500},
{firstName:'Mike',lastName:'Sina',company:'Company 3',hireDate:hireDate,salary:2900},
{firstName:'Jack',lastName:'Taylor',company:'Company 1',hireDate:hireDate,salary:7800},
{firstName:'Simon',lastName:'Portmann',company:'Company 2',hireDate:hireDate,salary:4700}],
newCompany,newEmployee;
companies.forEach(function(company){
newCompany = new ds.Company(company);
newCompany.save();
});
employees.forEach(function(employee){
newEmployee = new ds.Employee(employee);
newEmployee.company = ds.Company.find('name == :1',employee.company)
newEmployee.save();
});
ds.Employee.all()
속성 이름이 dataClass와 동일한 경우 생성시 개체를 할당하면됩니다. 당신이 그런 특별한 규정이있는 경우 또는 당신은 속성을 조작 할 수 있습니다 :
employees.forEach(function(employee){
newEmployee = new ds.Employee();
// Example how to assign value to an attribute on the import
// newEmployee is the new employee created in the dataStore of Wakanda.
// employee is the employee which come from the JSON object.
// Maybe the imported data have different attribute names.
newEmployee.firstName = employee.name;
newEmployee.lastName = employee.lname;
newEmployee.birthDate = new Date(employee.datestamp);
newEmployee.company = ds.Company.find('name == :1',employee.company)
newEmployee.save();
});
감사합니다, 나는 importFromJSON에게 나는이 시도 줄 것이다 – Darrellh