customrealms / core

Core library for the CustomRealms runtime
https://customrealms.io/core
MIT License
35 stars 13 forks source link

Server crashes when MongoDB is used #32

Closed SpreeHertz closed 1 year ago

SpreeHertz commented 2 years ago

Description

When MongoDB is used for saving data, the server crashes with an error (see below).

Code

src/main.ts

import { PlayerChatEvent, ServerCommands } from '@customrealms/core';
import { Schema, model, connect } from 'mongoose';

// First: Save the location
ServerCommands.register(`/savelocation {location}...`, async (player, call) => {
     // Get the coordinates
     const locationX = player.getTargetBlock()?.getX();
     const locationY = player.getTargetBlock()?.getY();
     const locationZ = player.getTargetBlock()?.getZ();
     // Get location name
     const locationName = call.getPlaceholder('location')!;

     interface ILocations {
          locationName: string,
          locationX: number,
          locationY: number,
          locationZ: number
     }
     // Make the schema
     const locationSchema = new Schema<ILocations>({
          locationName: { type: String, required: true },
          locationX: { type: Number, required: true }, 
          locationY: { type: Number, required: true },
          locationZ: { type: Number, required: true }

     })

     const Location = model<ILocations>('Locations', locationSchema);

     run().catch(err => console.log(err))

     async function run() {
          // Connect to MongoDB
          await connect("string");

          const loc = new Location({
            locationName: `${locationName}`,
            locationX: `${locationX}`,
            locationY: `${locationY}`,
            locationZ: `${locationZ}`

          });
          await loc.save();

        }

     console.log(`${player.getDisplayName()} has saved a location named "${locationName}". X: ${locationX} / Y: ${locationY} / Z: ${locationZ}`)

     player.sendMessage(`Your location named "${locationName}" has been saved.\nX: ${locationX} / Y: ${locationY} / Z: ${locationZ}`)
})

Error message:

image