无法从领域对象中检索数据

时间:2018-06-06 11:37:48

标签: react-native realm

我想知道从包含架构数据的领域对象中获取数据

db.js-Page,其中定义了realm数据库

  ..........................................

  export const tableProjectSchema = {

   name: TABLE_PROJECT,

   primaryKey: 'id',

   indexed:'true',

   properties: {

        id: 'int',

        project_id: 'int',

        project_name: 'string'
    }

};

 ...................................................

 export const  getProject = newProject => new Promise((resolve, reject) =>

 {  

 Realm.open(databaseOptions).then(realm => {

 realm.readOnly(() => {

 let allProjects = realm.objects(TABLE_PROJECT);

 resolve(allProjects);

});

}).catch(

  (error) => reject(error));

});

sample.js - 这里从这个页面导入了db.js和调用函数getProject()。

render() {

    let Projects=Database.getProject();

    console.log("table object"+Projects);
    ...................
    }

我想从对象' Projects'中获取project_name。在sample.js。

退出:   表对象[对象对象]

1 个答案:

答案 0 :(得分:0)

修改了getProject()方法。

export const  getProject = newProject => new Promise((resolve, reject) =>

 {  

     Realm.open(databaseOptions).then(realm => {

     let allProjects = realm.objects(TABLE_PROJECT);

     resolve(allProjects);

    }).catch(

    (error) => reject(error));

  });

使用

从对象中获取数据
 Database.getProject().then((projects) => 
 console.log(projects.forEach(project => console.log(`Project name: 
 ${project.project_name}`)))).catch((error) => { console.log(`Error in 
 fetching projects: ${error}`) });