【问题标题】:how to type an array of objects coming from realmDB?如何键入来自 realmDB 的对象数组?
【发布时间】:2021-07-27 13:16:19
【问题描述】:

错误:“Results”类型的参数不能分配给“SetStateAction”类型的参数。 “Results”类型不具有“Courses[]”类型的以下属性:pop、push、reverse、shift 等 6.ts(2345)

// my interface
interface Courses {
  id: string;
  name: string;
  image: string;
}

const [courses, setCourses] = useState<Courses[]>([]);

useEffect(() => {
    async function getOfflineCourses() {
      const realmDB = await realm;

      realmDB.write(() => {
        const offlineCourses = realmDB.objects<Courses[]>('Course');
        setCourses(offlineCourses);
      });
    }
    getOfflineCourses();
  }, [realm]);

【问题讨论】:

  • const offlineCourses: Courses[] = realmDB.objects('Course');
  • 我尝试过这种方式,但没有成功。错误:“Results”类型没有“Courses[]”类型的以下属性:pop、push、reverse、shift 等 6.ts(2740)
  • 这个问题有点不清楚,但如果您尝试从领域读取课程对象,这就是模式const courses = realm.objects("Courses");。话虽如此,命名有点混乱。领域对象是独立的实体,因此课程对象将被称为课程,而不是课程。还需要定义 Course 对象,但问题中的对象不是 Realm 对象的完整定义。你能澄清一下这个问题吗?

标签: typescript react-native realm


【解决方案1】:

我得到了我希望使用以下函数添加到我的状态的回报:

setCourses(offlineCourses.toJSON());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多