From 2e2561ff11c2993ed21a1c43dc28058ba30fb1d1 Mon Sep 17 00:00:00 2001 From: drillprop Date: Wed, 27 Feb 2019 21:06:20 +0100 Subject: [PATCH] switch to embeded data model --- src/graphql/resolvers/Mutation.js | 9 +++++---- src/models/Album.js | 4 ++-- src/models/User.js | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/graphql/resolvers/Mutation.js b/src/graphql/resolvers/Mutation.js index dab804a..632148e 100644 --- a/src/graphql/resolvers/Mutation.js +++ b/src/graphql/resolvers/Mutation.js @@ -1,9 +1,8 @@ -import fetch from 'node-fetch'; import bcrypt from 'bcryptjs'; +import 'dotenv/config'; +import jwt from 'jsonwebtoken'; import Album from '../../models/Album'; import User from '../../models/User'; -import jwt from 'jsonwebtoken'; -import 'dotenv/config'; const Mutation = { createCd: async (parent, args, ctx, info) => { @@ -18,8 +17,10 @@ const Mutation = { artist, image }); + await album.save(); await user.albums.push(album); - user.save(); + await user.save(); + return album; }, signup: async (parent, args, ctx, info) => { diff --git a/src/models/Album.js b/src/models/Album.js index c50059f..af8f377 100644 --- a/src/models/Album.js +++ b/src/models/Album.js @@ -1,6 +1,6 @@ -import { Schema, model } from 'mongoose'; +import { model, Schema } from 'mongoose'; -const albumSchema = new Schema({ +export const albumSchema = new Schema({ artist: String, title: String, image: String diff --git a/src/models/User.js b/src/models/User.js index 93b962c..7c33e58 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -1,4 +1,5 @@ -import { Schema, model } from 'mongoose'; +import { model, Schema } from 'mongoose'; +import { albumSchema } from './Album'; const userSchema = new Schema({ name: { type: String, unique: true, required: true }, @@ -6,7 +7,7 @@ const userSchema = new Schema({ password: { type: String, required: true }, date: { type: Date, default: Date.now }, avatar: String, - albums: [] + albums: [albumSchema] }); const User = model('Users', userSchema);