sheleoni / icecream-kana-game

A game for learning Japanese characters in the form of collecting ice-cream scoops. 🍦
https://icecream.sheleoni.com/
1 stars 0 forks source link

feat: create route handler (POST) for sending user game data #57

Closed sheleoni closed 9 months ago

sheleoni commented 9 months ago

Objective:

Issue:

but when we send this object as-is to mongoDB, the object is not recognised and only an _id is added to the document. image (ref)

Solution

Settled for now, documented here.

vercel[bot] commented 9 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
icecream-sheleoni-com ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2023 10:57am
sheleoni commented 9 months ago

Problem at: bd141d1

CleanShot 2023-12-10 at 16 02 13@2x tideLevel is being stored as ObjectId on mongodb, not sure how to go about this.

Tried changing tideLevelSchema to

tideLevel: {
    type: Map,
    of: Number
}

but it did not work.

Also tried using a mixed schema

import mongoose, { Schema, model, models } from "mongoose";

export const userTideLevelSchema = new Schema({
    // kana: String,
    // tideLevel: Number,
        type: mongoose.Schema.Types.Mixed

    },
);

but it did not work.

sheleoni commented 9 months ago

This may be related:

Maybe this will help:

sheleoni commented 9 months ago

OK it's probably a schema thing, eba40c0 seems to shed us some light! 😢

Achieved (on mongoDB): CleanShot 2023-12-10 at 17 01 24@2x

By trying out: CleanShot 2023-12-10 at 17 02 26@2x

        const clonedTideLevel = { kana: "ら", level: 3 };

and

export const userTideLevelSchema = new Schema({
    kana: String,
    level: Number,
    }
);
sheleoni commented 9 months ago

I guess my ultimate question is: store a object with many keys mongoDB or as array?

sheleoni commented 9 months ago

I think we may have to get use to the fact that frontend data structure can be different from backend data structures.

E.g. in the frontend, tideLevel.js:

const tideLevel =
    {
    // hiragana
    "あ": 5,
    "い": 4,
    "う": 3,
    "え": 2,
    "お": 1,
    "か": 5, ...and so on

In the backend: [{ kana: 'あ’, tideLevel: 5, _id: ObjectId('someId') }, { and so on }]

need to see if it's a code problem or not before moving on to this conclusion

sheleoni commented 9 months ago

Settled on this solution for now: b1e18c0 transforms object (e.g. { “あ”: 5, “い”: 2, “う” : 3, “え”: 1 } ) into e.g. [{ “あ”: 5 }, {“う” : 3} ] CleanShot 2023-12-13 at 19 58 22@2x