skip to Main Content

I am trying to get data from the db into node js but getting error . I have got the data from the frontend and stored it in the backend but when I try to get the stored data I get error.

React js:(frontend)

import React, { useEffect } from "react";
import axios from "axios";

export default function marketPlace(){

    useEffect(()=>{
        fetchingData();
    })

    const fetchingData = ()=>{
        axios.get("http://localhost:4040/api/nftdata")
        .then(res=>{
            console.log(res)
        })
        .catch(err=>{
            console.log(err)
        })
    }


    return(
        <div>
            MArket Place
        </div>
    )
}

Backend:

index.js:
var express = require("express");
var app =express()
var cors = require("cors");
var bodyparser = require("body-parser");
var router = require('./router/router');
const connectDB = require('./db/db');


require('./sevice/transactionChecker')(app);
connectDB();

var PORT = 4040

app.use(cors());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:false}));

app.use('/api', router);

app.listen(PORT,()=>{
    console.log(`Listening in ${PORT}`)
})


module.exports = app;

router.js:

var express = require("express")
var router = express.Router();
//var app = express();

const aadiTransaction = require('../controller/aadiTransaction');
const nftIds = require('../controller/nftIds');
const nftData = require('../controller/nftIds');
const ids = require('../controller/nftdata');

router.get("/", (req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.json({
    code: 200,
    dismsg: "Api is Running...",
  });
});

router.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
  });

router.post('/ids', nftIds)
router.get('/inserttrans/:coinpair/:tx/:tokencount', aadiTransaction)
router.get('/nftdata', nftData)

module.exports = router;

schema:

const mongoose = require("mongoose");

const ids =  new mongoose.Schema(
   {
    id: {
        type: String,
        required: true
    },
    name:{
        type:String,
        required: true
    },
    description:{
        type:String,
        required: true
    },
    imageUrl:{
        type: String,
        required: true
    },
    price:{
        type: Number,
        required: true
    },
    videoUrl:{
        type: String
        
    }
   } 
)

module.exports = mongoose.model("contacts", ids);

nftData.js :

const mongoose = require('mongoose')
require('dotenv').config()

const ids = require('../modals/nftId')

const nftData = async(req,res)=>{
    
    await ids.find({})
    .then((data)=>{
        console.log(data)
        res.json(data)
    })
    .catch((err)=>{
        console.log(err)
    })
}

module.exports = nftData;

I have data in my database when I try to get the data I get these errors.

Error: contacts validation failed: id: Path `id` is required., name: Path `name` is required., description: Path `description` is required., imageUrl: Path `imageUrl` is required., price: Path `price` is required.
    at ValidationError.inspect (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooseliberrorvalidation.js:50:26)  
    at formatValue (node:internal/util/inspect:806:19)
    at inspect (node:internal/util/inspect:365:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2292:40)
    at formatWithOptions (node:internal/util/inspect:2154:10)    
    at console.value (node:internal/console/constructor:339:14)  
    at console.log (node:internal/console/constructor:376:61)    
    at C:UsersgauthOneDriveDesktopmintservercontrollernftIds.js:32:13
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: {
    id: ValidatorError: Path `id` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'id',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    name: ValidatorError: Path `name` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'name',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    description: ValidatorError: Path `description` is required. 
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'description',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    imageUrl: ValidatorError: Path `imageUrl` is required.       
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'imageUrl',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    price: ValidatorError: Path `price` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaNumber.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'price',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'contacts validation failed'
}
Error: contacts validation failed: id: Path `id` is required., name: Path `name` is required., description: Path `description` is required., imageUrl: Path `imageUrl` is required., price: Path `price` is required.
    at ValidationError.inspect (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooseliberrorvalidation.js:50:26)  
    at formatValue (node:internal/util/inspect:806:19)
    at inspect (node:internal/util/inspect:365:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2292:40)
    at formatWithOptions (node:internal/util/inspect:2154:10)    
    at console.value (node:internal/console/constructor:339:14)  
    at console.log (node:internal/console/constructor:376:61)    
    at C:UsersgauthOneDriveDesktopmintservercontrollernftIds.js:32:13
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: {
    id: ValidatorError: Path `id` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'id',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    name: ValidatorError: Path `name` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'name',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    description: ValidatorError: Path `description` is required. 
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'description',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    imageUrl: ValidatorError: Path `imageUrl` is required.       
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'imageUrl',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    price: ValidatorError: Path `price` is required.
        at validate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1346:13)
        at SchemaNumber.SchemaType.doValidate (C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibschematype.js:1330:7)
        at C:UsersgauthOneDriveDesktopmintservernode_modulesmongooselibdocument.js:2903:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'price',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'contacts validation failed'
}

2

Answers


  1. You need to add the backend url "http://localhost:4040&quot; as a proxy to the ‘package.json’ file in frontend folder i.e. React project folder

    // package.json
    proxy: "http://localhost:4040"
    
    Login or Signup to reply.
  2. check these lines of code in backend/router.js

    const nftIds = require('../controller/nftIds');
    const nftData = require('../controller/nftIds');
    

    you are requiring the same controllers for two different purposes i guess

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search