Closed sonaskyla12 closed 6 years ago
This issue tracker is for reporting bugs in node core and submitting feature requests for node core.
General help questions should be posted to the nodejs/help issue tracker instead.
Issues with third-party modules, npm, or other tools that use node, should be posted to the appropriate issue tracker for that project, unless it can be proven that the issue is in fact with node core and not the module/tool in question.
basically i have two folder in the root...the structure in like this in the root
models |----User.js routes |----api |----users.js 1.The code under User.js is:-
const mongoose = require('mongoose'); const Schema = mongoose.Schema; //create schema const UserSchema = new Schema({ name: { type: String, required: true }, email: { type: String, required: true }, password: { type: String, required: true }, avatar: { type: String }, date: { type: Date, default: Date.now },
}); module.exports = User = mongoose.model('user', UserSchema); 2.The code under users.js const express = require('express'); const router = express.Router(); const gravatar = require('gravatar'); const bcrypt = require('bcryptjs'); //load user model const User = require('../../models/User'); router.get('/test', (req, res) => res.json({msg: 'user works'})); //route Get api/user/register router.post('/register',(req,res) =>{ User.findOne({email : req.body.email}) .then(user =>{ if(user){ return res.status(400).json({email:'Email already exist'}); }else{ const avatar =gravatar.url(req.body.email,{ s:'200', r:'pg', d:'mm' } ); const User = new User({ name:req.body.name, email:req.body.email, avatar, password:req.body.password
}) });
module.exports = router; 3.postman is giving the suitable output on htttp://localhost:5000/api/users/test but not giving the output for http://localhost:5000/api/users/register now it is showing the error:-
error that is coming after this are:-
error (node :7408) UnhandledPromiseRejectionWarning ReferenceError: User not defined
at User.findone.then.user(C:\Users\shubhendu\desktop\mernstack\routes\api\users.js:20:20)
(node:7408) UnhandledPromiseRejectionWarning : Unhandled promise rejection . this error orginated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch . (rejection id:1)
(node :7408) [ DepricationWarning : Unhandled promise rejections are deprecated .In future , promise rejectionsthat are not handled will terminate the nodejs process with thenon zero exit code
could you help me how to resolve it..