Below is the my mongodb schema Product with six fields - username, email, bio, image, hash and salt :
var mongoose = require("mongoose");
var productSchema = new mongoose.Schema(
{
username: {
type: String,
lowercase: true,
required: [true, "can't be blank"],
index: true,
},
email: {
type: String,
lowercase: true,
required: [true, "can't be blank"],
index: true,
},
bio: String,
image: String,
hash: String,
salt: String,
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", productSchema);
My Data/Product.js Info, data stored in the database:
[
{
username: "USERNAME",
email: "name.username@gmail.com",
bio: "BIOSample",
image: "Image",
hash: "sample",
salt: "sample",
},
];
My Controller Code:
const Product = require("../models/product");
//create new product => /api/v1/product/new
exports.newProduct = async (req, res, next) => {
const product = await Product.create(req.body);
res.status(201).json({
success: true,
product,
});
};
While npm run I get this error message as shown below:
Product validation failed: email: can't be blank, username: can't be blank.