Sorry for the long post. It's mostly code, and I'm not sure if that makes it better or worse.I'm attempting to create a resume website using TypeORM. The idea is that I have a position listed, along with a description, and a list of skills related to that job. My database is structured like this:// src/entity/Content.ts import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from "typeorm"; import { Skill } from './Skill' @Entity() export class Content { @PrimaryGeneratedColumn() id: number; @Column({type: 'text', unique: true}) title: string; @Column('text') content: string; @ManyToMany(type => Skill, skill => skill.title, { cascade: true } ) @JoinTable() skills: Skill[]; } // src/entity/Skill.ts import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from "typeorm"; import { Content } from './Content' @Entity() export class Skill { @PrimaryGeneratedColumn() id: number; @Column({type: 'text', unique: true}) title: string; @ManyToMany(type => Content, content => content.title) content: Content[]; } My UI allows for any number of skills to be added at a time, so the backend looks like this:import "reflect-metadata"; import { createConnection } from "typeorm"; import { Content } from "./entity/Content"; import { Skill } from "./entity/Skill"; import * as express from "express"; import * as bodyParser from "body-parser"; createConnection().then(async connection => { const app = express(); app.use(bodyParser.json()); let contentRepository = connection.getRepository(Content); let skillRepository = connection.getRepository(Skill); // ... remove extraneous code app.post('/', async function(req, resp) { if (! await contentRepository.findOne({ title: req.body.title } )) { let skills; if (req.body.skills.length) { skills = req.body.skills.map((skill) => { if ( skill.checked === true ) { const s = new Skill() s.title = skill.title return s } }); } const content = new Content(); content.title = req.body.title content.content = req.body.content; let savedSkills; if (skills.length) { savedSkills = await skills.map((skill) => { skillRepository.save(skill).then((s) => { content.skills = content.skills || []; content.skills.push(s); console.log(content.skills); }); }); } console.log(await savedSkills); await contentRepository.save(content).catch((err) => { console.log(err); resp.send(JSON.stringify({error: err })); }); resp.send(JSON.stringify({ resp: 'new post saved' })); return; } resp.send(JSON.stringify( {error: "Oops, There was an error." } )); }); app.listen(3000) }).catch(error => console.log(error)); The issue is that the skills aren't being associated with the content. They're being saved, but then when I query the database again the relations don't exist. I get [title:'title', content: 'content' skills: []] (skills ends up being an empty array). If this were synchronous code this would work perfectly. What do I need to do to make it work async? I've tried adding await in various places, but nothing seems to work.
Submitted January 18, 2020 at 04:10AM by yramagicman
No comments:
Post a Comment