I have a user schema that gets the data from the random user api. I use some of that data and add it into a schema, no problem doing that. But i want to add a new prop into the json called score that is a random number between 7.5 and 10. By doing this i get that the validation for the score prop in the schema failed.
Here is my schema:
const mongoose = require("mongoose");
const PersonSchema = new mongoose.Schema({
gender: {
type: String,
required: [true, "You must enter a gender"],
},
name: {
first: {
type: String,
required: [true, "You must enter a first name"],
},
last: {
type: String,
required: [true, "You must enter a last name"],
},
},
email: {
type: String,
required: [true, "You must enter an email"],
},
dob: {
age: {
type: Number,
required: [true, "You must enter an age"],
},
},
score: {
type: Number,
required: [true, "You must enter a score"],
},
});
module.exports = mongoose.model("Person", PersonSchema);
And here is my populate db file:
require("dotenv").config();
const { default: axios } = require("axios");
const PersonModel = require("./models/person");
const connect = require("./db/connect");
const url = "https://randomuser.me/api";
function generateUserJson() {
const promise = axios.get(url);
const dataPromise = promise.then((response) => response.data);
return dataPromise;
}
const generateRandomScore = (min, max) => {
return Math.random() * (max - min) + min;
};
generateUserJson()
.then((data) => {
insertUserIntoDb(data.results);
})
.catch((err) => console.log(err));
const insertUserIntoDb = async (data) => {
data.score = generateRandomScore(7.5, 10);
console.log(data);
try {
await connect(process.env.MONGO_URI);
await PersonModel.deleteMany();
await PersonModel.create(data);
console.log("User added");
} catch (err) {
console.log(err);
}
};
The json after adding the score prop looks like this:
[
{
gender: 'male',
name: { title: 'Mr', first: 'Eetu', last: 'Manni' },
location: {
street: [Object],
city: 'Nilsiä',
state: 'Northern Savonia',
country: 'Finland',
postcode: 77242,
coordinates: [Object],
timezone: [Object]
},
email: '[email protected]',
login: {
uuid: '39d2687b-7af8-4203-9b34-f05d2ab74bc8',
username: 'brownfish992',
password: 'request',
salt: 'DO7Gjdn2',
md5: '49bfe3910055219f30a30f82b8683e71',
sha1: 'd3a75a6653ed3e032b1683a419ebd6960c075d5a',
sha256: '4ee03ac4d668f1b5ebf2187cfe0f223be422fb1daadb365566616a14d7844a32'
},
dob: { date: '1947-02-11T18:26:04.592Z', age: 75 },
registered: { date: '2008-07-02T05:41:34.752Z', age: 14 },
phone: '03-674-277',
cell: '045-658-75-87',
id: { name: 'HETU', value: 'NaNNA661undefined' },
picture: {
large: 'https://randomuser.me/api/portraits/men/86.jpg',
medium: 'https://randomuser.me/api/portraits/med/men/86.jpg',
thumbnail: 'https://randomuser.me/api/portraits/thumb/men/86.jpg'
},
nat: 'FI'
},
score: 9.394881677107008
]
And the error message looks like this: Error: User validation failed: score: You must enter a score at ValidationError.inspect
Usermodel, but the schema you posted is forPerson. It's confusing, did you added wrong code snippet?scoreproperty is outside the JSON object - outside the closing curly brace,