These are chat archives for Automattic/mongoose

12th
May 2018
Ben Chiciudean
@benydc
May 12 2018 12:50
I need help with an issue
when I try to do operarion with Schema I have defined already, I get this error:
ReferenceError: slide_item_schema_1 is not defined
Kev
@lineus
May 12 2018 12:52
@benydc bow ties are cool :)
can you show an example?
Ben Chiciudean
@benydc
May 12 2018 12:53
import mongoose from "mongoose";
import { ISlideItemModel } from "../models/slide-item.model"; //import IUserModel

let schema: mongoose.Schema = new mongoose.Schema({
    image: String,
    firstTitle: String,
    secondTitle: String,
    innerText: String,
    actionText: String,
    url: String,
    createdAt: { type: Date, default: Date.now, index: true }
}).pre("save", function (next) {
    //this.updated = Date.now();
    next();
});

export let SlideItem = mongoose.model<ISlideItemModel>('SlideItem', schema, 'slideitems', true);

My schema definition

And where I’m using it:

import { SlideItem } from '../schemas/slide-item.schema';

export const index = async (ctx) => {
    ctx.body = await SlideItem.find().catch(error => console.log(error));
}
Kev
@lineus
May 12 2018 13:05
@benydc is there a stack trace? if so, can you share that as well?
Ben Chiciudean
@benydc
May 12 2018 13:11
with console.trace() ?
Kev
@lineus
May 12 2018 13:14
what you shared briefly was fine
I don't use TS so some of your code is foreign to me, though it seems reasonable. what I was hoping to get from the stack trace was whether you were getting that error from mongoose or your code.
Ben Chiciudean
@benydc
May 12 2018 13:24
could it be the db itself?
the mongo I’m running locally
Kev
@lineus
May 12 2018 13:28
I don't think so, is slide_item_schema_1 defined in the transpiled code?
@benydc if your code is checked into a publicly accessible repo in it's current state, share the link, and I'll take a stab at it.
Ehsan Shadmehri
@ehsansh
May 12 2018 16:02

Hi I am using mongodb and mongoose for my project. I Have a question. What is the best practice for designing schemas for user and user'purchases? I have two solutions The first one is that I make one schema for users and have the purchase in the user schema like this:

var userSchema = new mongoose.Schema({
    username: {
        type: String,
        unique: true,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    purchase:[
        {
            item:String
        }
    ]
});

In the second approach I make two schemas one for user information and another one like this for purchases:

var purchaseSchema = new mongoose.Schema({
    user_id:String,
    purchase:[
        {
            item:String
        }
    ]
});

Which one is better and why?

Arun Gadag
@arun-awnics
May 12 2018 16:37
Definitely the second approach. Keep each dataset separately and then link them using a primary key(or _id in case of mongo)
Ben Chiciudean
@benydc
May 12 2018 16:42
@lineus hey, I found the problem

tsc did not compile properly, and after I run a manual build it worked.

Object.defineProperty(exports, "__esModule", { value: true });
const slide_item_schema_1 = require("../schemas/slide-item.schema");
exports.index = (ctx) => __awaiter(this, void 0, void 0, function* () {
    ctx.body = yield slide_item_schema_1.SlideItem.find().catch(error => console.log(error));
});

You can see slide_item_schema_1 is degined properly now…

Kev
@lineus
May 12 2018 16:45
@benydc nice!
Ehsan Shadmehri
@ehsansh
May 12 2018 19:32
@arun-awnics Thanks but this article has suggested the first approach too. https://www.tutorialspoint.com/mongodb/mongodb_relationships.htm What is your idea?
Ehsan Shadmehri
@ehsansh
May 12 2018 19:41
@arun-awnics Also one answer here: https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference suggests to eliminate joins.