I am trying to search a file when I type something in the form. first I do POST and save it, then in the other form, I search , if the input is equal that I have in the database, I fecth it, else It do nothing .
I am getting "not find" when I execute it in postman. Can someone help me? I aprecciate
const router = require("express").Router();
const Post = require ("../models/Post");
router.get("/" ,async (req,res ) => {
try {
const findPost = await Post.findOne();
if ( req.body.post === findPost) {
res.status(200).json(findPost);
} else {
res.status(401).json("not find")
}
} catch {
res.status(500).json("not correct");
}
this is the post Schema
const mongoose = require("mongoose")
const PostSchema = new mongoose.Schema({
post: {
type: String,
required: true,
}
}, {timestamps: true});
module.exports = mongoose.model("Post", PostSchema)
// A simple solution
const findPost = await Post.findOne({post:req.body.post});
if(findPost){
res.status(200).json(findPost);
}
else{
res.status(401).json("not found");
}
2.If you are willing to filter all posts matching a regEx
// If you want to match pattern disregarding cashes
const regEx = new RegExp(req.body.post, "i");
const allPosts = await Post.find({post: regEx});
if(allPosts && allPosts.length){
res.status(200).json(allPosts);
}
else{
res.status(401).json("not found");
}