0

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

4
  • Am I missing something? You are adding data to User model, but the schema you posted is for Person. It's confusing, did you added wrong code snippet? Commented Mar 9, 2022 at 20:07
  • My bad, @ShivamSood, i changed some names to post it here and fu*** up. That's not the problem tho, that's just an editing problem that i made copying the original code. Already edited. Commented Mar 9, 2022 at 20:12
  • Might have to do with numberLong? stackoverflow.com/questions/8617272/… Commented Mar 9, 2022 at 20:59
  • The score property is outside the JSON object - outside the closing curly brace, Commented Mar 10, 2022 at 4:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.