100xdevs-cohort-2 / paytm

223 stars 455 forks source link

Balance not showing in the get request in account/balance #32

Open baldanaresh opened 5 months ago

baldanaresh commented 5 months ago

the error im getting is :SyntaxError: Unexpected end of JSON input at JSON.parse () at createStrictSyntaxError (C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\body-parser\lib\types\json.js:160:10) at parse (C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\body-parser\lib\types\json.js:83:15) at C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\body-parser\lib\read.js:128:18 at AsyncResource.runInAsyncScope (node:async_hooks:203:9) at invokeCallback (C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\raw-body\index.js:231:16) at done (C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\raw-body\index.js:220:7) at IncomingMessage.onEnd (C:\Users\nares\OneDrive\Desktop\Paytm\paytm\backend\node_modules\raw-body\index.js:280:7) at IncomingMessage.emit (node:events:514:28) at endReadableNT (node:internal/streams/readable:1359:12)

My code:const express=require("express"); const router=express.Router(); const zod=require("zod"); const {user}=require("../db"); const jwt=require("jsonwebtoken"); const {JWT_SEC}=require("../config"); const {authMiddleware}=require("../middleware")

const signupSchema=zod.object({ username:zod.string().email(), password:zod.string(), firstname:zod.string(), lastname:zod.string(), });

router.post("/signup",async(req,res)=>{ const body=req.body; const {success}=signupSchema.safeParse(req.body); if(!success){ return res.status(400).json({ message:" incorrect inputs" }) } const existingUser= await user.findOne({ username:body.username }) if(existingUser){ return res.status(409).json({ message:"Email already taken" }) }

const dbUser=await user.create({
    username:body.username,
    password:body.password,
    firstname:body.firstname,
    lastname:body.lastname,
});
const token=jwt.sign({
    userId:dbUser._id
},JWT_SEC)
res.status(201).json({
    message:"User created successfully",
    token:token
})

});

const signinBody = zod.object({ username: zod.string().email(), password: zod.string() }) router.post("/signin",async(req,res)=>{ const body=req.body; const {success}=signinBody.safeParse(body); if(!success){ return res.status(400).json({ message:" incorrect inputs" }) } const userRecord = await user.findOne({ username: req.body.username, password: req.body.password });

if (userRecord) {
    const token = jwt.sign({
        userId: userRecord._id
    }, JWT_SEC);

    res.json({
        token: token
    })
    return;
}
res.status(401).json({
    message: "Error while logging in"
})

}) const updateBody = zod.object({ password: zod.string().optional(), firstName: zod.string().optional(), lastName: zod.string().optional(), })

router.put("/", authMiddleware, async (req, res) => { const { success } = updateBody.safeParse(req.body) if (!success) { res.status(400).json({ message: "Error while updating information" }) }

await user.updateOne(req.body, {
    id: req.userId
})

res.json({
    message: "Updated successfully"
})

}) router.get("/bulk", async (req, res) => { const filter = req.query.filter || "";

const users = await user.find({
    $or: [{
        firstName: {
            "$regex": filter
        }
    }, {
        lastName: {
            "$regex": filter
        }
    }]
})

res.json({
    user: users.map(user => ({
        username: user.username,
        firstName: user.firstname,
        lastName: user.lastname,
        _id: user._id
    }))
})

})

module.exports=router;

chirag-63 commented 5 months ago

can u please provide your accounts file. above code does not have any kind of get request in account/balance.

yesahem commented 3 months ago

Push the code to GitHub and send the repo link

Happy to check the issue and resolves it

baldanaresh commented 3 months ago

Push the code to GitHub and send the repo link

Happy to check the issue and resolves it

Hi yesahem ,

I hope you're doing well. I have pushed the latest code to the repository. You can check it out here: https://github.com/baldanaresh/Paytm-basic.git

Thank you so much for your attention to this issue. Please take a look and let me know if you encounter any problems or have further questions.

yesahem commented 3 months ago

Hey Naresh, So as I was going through your codebase I found that when a user is visiting the /api/v1/user/signup route it is not creating a Account in the db resulting no user account creation and if the account is not created then how balance can be shown to you...

I am pushing the code feel free to review it and merge it

For any query feel free to ping me on twitter : https://x.com/heyshishu Happy Coding ❤️