Thursday 29 August 2019

Help with Mongoose Schema with existing Collection

A friend of mine wants a simple site to view some statistics that he has gathered. He has saved the data using mongodb and now I'm trying to create a simple api so that I can create a front end with the information (This is a sample of what each document looks like https://pastebin.com/KmxGSkPy)​Here is my issue, I tried creating a mongoose Schema which currently looks like this:const mongoose = require('mongoose');const profileSchema = mongoose.Schema({_id: mongoose.Schema.Types.ObjectId,uuid: String,name: String,essentials: { nick: String },options: {showScoreboard: Boolean, allowSpectators: Boolean, receiveDuelRequests: Boolean}});module.exports = mongoose.model('Profile', profileSchema);If you looked at the sample data I provided you can see that this is missing kitStatistics and loadouts, but this still works and gives back all of the information from my api running this code:app.get('/api', (req, res) => {Profile.find({name: "Irantwomiles"}, function(err, profile) {if(err) {res.send("error");}res.json(profile);})})Note, that the current name of the file is profile.js. Now if I were to rename this file to anything else, the api just returns and empty array [] . So my question is why does this work when its named profile, but then it doens't work when its named anything else? Does a schema need to match exactly to what I have in my collection?

Submitted August 29, 2019 at 09:38PM by Irantwomiles

No comments:

Post a Comment