我正在尝试设置角度4以使用某些课程填充Firebase数据库。每门课程都有课程。在每节课中都有笔记。
我面临以下问题:
我已将评论直接包含在问题所在的代码段上方。任何帮助将不胜感激。
DB-data.ts
export const dbData = {
"courses": [
{
"url": "getting-started-with-angular4",
"description": "Angular 4 Tutorial For Beginners",
"lessons": [
{
"url": “angular4-write-hello-world”,
"description": “Write your first Hello World”,
// not sure if notes is correctly nested
“notes”: [
{
“url”: “angular4-hello-world”,
“description”: “angular4-hello-world-notes”
}
]
}
]
}
]
};
填入-db.ts
import 'zone.js';
import 'reflect-metadata';
import {database, initializeApp} from "firebase";
import {firebaseConfig} from "./src/environments/firebase.config";
import {dbData} from "./db-data";
initializeApp(firebaseConfig);
const coursesRef = database().ref('courses');
const lessonsRef = database().ref(‘lessons');
const notesRef = database().ref(‘notes’);
dbData.courses.forEach( course => {
const courseRef = coursesRef.push({
url: course.url,
description: course.description
});
let lessonKeysPerCourse = [];
course.lessons.forEach((lesson:any) => {
console.log('adding lesson ', lesson.url);
// not sure if lessonRef is correctly assigned
const lessonRef = lessonKeysPerCourse.push(lessonsRef.push({
url: lesson.url,
description: lesson.description,
courseId: courseRef.key
}).key);
});
const association = database().ref('lessonsPerCourse');
const lessonsPerCourse = association.child(courseRef.key);
lessonKeysPerCourse.forEach(lessonKey => {
const lessonCourseAssociation = lessonsPerCourse.child(lessonKey);
lessonCourseAssociation.set(true);
});
let noteKeysPerLesson = [];
// Error “cannot find name 'lesson'. any”
lesson.notes.forEach((note: any) => {
noteKeysPerLesson.push(notesRef.push({
url: note.url,
description: note.description,
lessonId: lessonRef.key
}).key);
});
const associationNoteLesson = database().ref(‘notesPerLesson');
// Error “cannot find name 'lessonRef'. any”
const notesPerLesson = associationNoteLesson.child(lessonRef.Key);
noteKeysPerLesson.forEach(noteKey => {
const noteLessonAssociation = notesPerLesson.child(noteKey);
noteLessonAssociation.set(true);
})
});