Home > Back-end >  Trying to use mongoose with vue and getting error
Trying to use mongoose with vue and getting error

Time:09-12

any time i try to import this the page throws the error of

Uncaught TypeError: Cannot read properties of undefined (reading 'split')

import { User } from '@/assets/schemas'

export default {
  name: 'HomeView',
  mounted() {
    //const user = User.findOne({ id: '1002401206750150836' })
    console.log('user')
  }
}

when i comment out the import it works but when i add the import back i get that error this is the code for the Schemas.js

const mongoose = require('mongoose');

const User = new mongoose.Schema({
    id: { type: String, unique: true, required: true},
    bank: { type: Number, default: 2000 },
    wallet: { type: Number, default: 0},
    chips: { type: Number, default: 0},
    level: { type: Number, default: 1},
    totalxp: { type: Number, default: 0},
    xp: { type: Number, default: 0},
    favcolor: { type: String, default: "White"},
    cooldowns: {
        daily: { type: Date },
        monthly: { type: Date },
        buychips: { type: Date },
    }
})

const Guild = new mongoose.Schema({
    id: { type: String, unique: true, required: true},
    welcome_channel_id: { type: String, default: null },
    new_member_role_id: { type: String, default: null }
})

module.exports = { User: mongoose.model("User", User), Guild: mongoose.model("Guild", Guild) }

CodePudding user response:

Mongoose is not a frontend library, it can not be used with Vue. It relies on Node.js functionality which doesn't exist in the browser. Mongoose is only meant to be used on a backend Node.js server.

  • Related