Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baldanaresh opened this issue Jun 4, 2024 · 4 comments
Open

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

baldanaresh opened this issue Jun 4, 2024 · 4 comments

Comments

@baldanaresh
Copy link

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
Copy link

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

@yesahem
Copy link

yesahem commented Jul 28, 2024

Push the code to GitHub and send the repo link

Happy to check the issue and resolves it

@baldanaresh
Copy link
Author

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
Copy link

yesahem commented Jul 29, 2024

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 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants