I am using two models for my app here are the models
var mongoose=require("mongoose");
var Schema = mongoose.Schema;
var schema = new Schema({
content:{type:String, required:true},
user:{type:mongoose.Types.Objectid, ref:'User'}
});
module.exports=mongoose.model('message',schema);
var mongoose=require("mongoose");
var Schema = mongoose.Schema;
var schema = new Schema({
firstName:{type:String,required:true},
lastName:{type:String,required:true},
password:{type:String,required:true},
email:{type:String, required:true,unique:true},
});
My two questions are as follow:
1 As for the Schema for every field like firstName I make it required: true So When I use Postman if I don't provide the firstName I will get an error that field required. My question is this can anyone give me a little snippet from the front end if I do not provide the firstname it will give me an error but wait this required is on the back end so how can I get the error on frontend if I don't provide the firstname.
2 Actually what I am doing that when some user sends a message so I get his message and from User collection, I get his id name and last name using reference I know how to do it with postman but I am actually confused about how I can do it from frontend using angular2+. Can anyone help me in this regard providing me with a little front end interface or some code snippet with some explanation which can do this operation?
so it looks like you're probably a bit new to full-stack development, no worries both of these are quite straight forward.
- Validation of fields in the frontend and backend
You should be validating input on both the frontend and the backend. On the frontend, you can make use of Angular to check if a field is null or empty, or even run regex checks to see if it's the valid format you expect. How you do this will greatly depend on what your frontend HTML is like. If you want further advise on this I'd suggest opening a new question specifically to address this on the frontend. However, you will find many answers if you just search Stackoverflow for "angular 7 validate input".
To validate on the backend, you will likely want some code that sits inside your express.js endpoint handler. You can check the value that comes through the body, do any checks against it. If it passes then continue on to create your database record, if it doesn't then return an error. If your application is sufficiently large you may also wish to run checks closer to your database models but for now validating at the edge may get you far enough if it's a very small application.
Here is an example of the sort of express.js validation you might use:
router.post("/message", (req, res, next) => {
if (!req.body.firstName) {
return res.status(400).json({error: "a meaningful error message"});
}
// The validation has passed, do whatever you want now
});
- Frontend HTTP requests in angular 2+
Making a HTTP call in angular 2 is reasonably straight forward and the documentation provides a full guide on making http calls which I would suggest following.
If you have specific questions or issues with it then feel free to come back and open another question, but since you've not provided the code of your current attempt it's hard to give an answer which will fit your code.