Getting Empty Object during Put request to the server - javascript

I created an API for following and followers user for Social Media Application, while request from postman getting empty object: {} But it seems to me to be correct.
Model:
const mongoose = require("mongoose");
const UserSchema = mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
firstname: {
type: String,
required: true,
},
lastname: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
profilePicture: String,
coverPicture: String,
about: String,
livesin: String,
workAt: String,
relationship: String,
followers: [],
following: []
},{
timestamps:true
}
);
const UserModel = mongoose.model("Users", UserSchema);
module.exports = UserModel;
UserControler:
const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");
const followUser = async (req, res) => {
const id = req.params.id.trim();
const { currentUserId } = req.body;
if (currentUserId === id) {
res.status(403).send("Action forbiden");
} else {
try {
const followUser = await UserModel.findById(id);
const followingUser = await UserModel.findById(currentUserId);
if (!followUser.followers.includes(currentUserId)) {
await followUser.updateOne({ $push: { followers: currentUserId } });
await followingUser.updateOne({ $push: { following: id } });
res.status(200).send({message:"User Followed"});
} else {
res.status(403).send("User alredy followed by you!");
}
} catch (error) {
res.status(500).send(error);
}
}
};
module.exports = { getUser, updateUser, userDelete, followUser };
UserRoute:
const express = require("express");
const {getUser,updateUser, userDelete, followUser} = require("../Controller/userControler");
const router = express.Router()
router.get("/:id",getUser)
router.put("/:id",updateUser)
router.delete("/:id", userDelete)
router.put("/:id/follow", followUser)
module.exports=router;
index.js:
app.use("/user",UserRoute)
Here is the complete details regarding the error, let me know what happens in the code, thank you.

i assume that you have all the other functions other than followUser in your controller.js
The thing is that you must first specify the field name on the basis of which you want to update the document.
Here is what you need to do;
const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");
const mongoose = require("mongoose");//updated line
const followUser = async (req, res) => {
const id = req.params.id.trim();
const { currentUserId } = req.body;
if (currentUserId === id) {
res.status(403).send("Action forbiden");
} else {
try {
const followUser = await UserModel.findById({_id: mongoose.Types.ObjectId(id)});
const followingUser = await UserModel.findById({_id: mongoose.Types.ObjectId(currentUserId)});
if (!followUser.followers.includes(currentUserId)) {
await followUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)},{ $push: { followers: currentUserId } });
await followingUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)}{ $push: { following: id } });
res.status(200).send({message:"User Followed"});
} else {
res.status(403).send("User alredy followed by you!");
}
} catch (error) {
res.status(500).send(error);
}
}
};
module.exports = { getUser, updateUser, userDelete, followUser };
And while hitting the api pls make sure that your route should be
localhost:port-number/user/12345789/follow
and also make sure that the API type in postman must be same as in the backend e.g; PUT

please try findByIdAndUpdate query insted of using updateOne

Related

How does a function writer call the getByName . function

books-controllers
I want the data to appear by the name in the postman and not the ID because I have information and I want to fetch it through the name in the database
const getByName = async (req, res, next) => {
const name = req.params.name;
let book;
try {
book = await Book.getByName("name");
} catch (err) {
console.log(err);
}
if (!book)
return res.status(404).json({ message: "No book found" });
}
return res.status(200).json({ book });
};
modelSchema
Here is the Skyma model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bookSchema = new Schema({
name: {
type: String,
require: true
},
description: {
type: String,
require: true
},
price: {
type: Number,
require: true
},
avilable: {
type: Boolean,
},
image: {
type: String,
require: true
},
});
module.exports = mongoose.model("Book", bookSchema);
There in no in-built method in mongoose getByName. You can use generic find to search for object using name
let book = await Book.find({ name: name }).exec();
You can also use findOne if needed.
You can try this -
async function getByName(req, res){
const bookname = req.params.name ;
try {
const book = await Book.findOne({name: bookname})
if(book!==null) {
res.status(200).send({'data': book}) ;
}
else {
res.status(404).send("No book found !")
}
}
catch(error) {
console.log(error)
res.send("Error")
}
}

Am I using the Repository pattern correctly in NodeJS?

I use Sequelize to work with the database.
In my project, I encountered code duplication, and decided to study the repository design pattern, which will separate the work with the database and data output from the business logic. Having studied the information on the Internet, I decided to consolidate the material and check with more experienced programmers. I use the repository pattern correctly?
controller/user.controller.js
const userService = require('../services/user.service');
exports.userCreate = async (req, res, next) => {
try {
const user = await userService.userCreate(req.body);
return res.status(201).json(user);
} catch (e) {
return next(e);
}
}
services/user.service.js
const ApiError = require('../utils/error');
const userRepo = require('../repositories/user.repository');
exports.userCreate = async (data) => {
if (!data) throw ApiError.badRequest('Bad');
const { login } = data;
if (!login) throw ApiError.badRequest('Bad');
const password = 'hash';
const user = await userRepo.userCreate({login, password});
return user;
}
repositories/user.repository.js
const User = require('../models/User');
exports.userCreate = async (data) => {
const { login, passowrd } = data;
const user = await User.create({login, password});
return {
login: user.login,
}
}
models/User.js
const { sequelize, Sequelize } = require('../config/db');
const User = sequelize.define('users', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
login: {
type: Sequelize.STRING,
allowNull: false,
},
password: {
type: Sequelize.STRING,
allowNull: false,
}
});
module.exports = User;
Your User model is fine, but the structure of the service seems a little weird. if all three of the functions userCreates are in separate files I'd recommend you combine them and rename services to Controllers, as services are usually used on the client side of things.
services/user.service.js
const User = require('../models/User')
// const userRepo = require('../repositories/user.repository') Not sure what this does
const ApiError = require('../utils/error')
module.exports = {
async createUser (req, res) {
try {
if (!req.body) throw ApiError.badRequest('Bad')
const { login } = req.body
if (!login) throw ApiError.badRequest('Bad')
const password = 'hash'
const user = await User.create({ login, password })
res.stauts(201).json(user.login)
} catch (err) {
console.log(err)
res.staus(500).send({
error: err
})
// return next(err)
}
}
}

Can't pass multiple documents in an array when using findById

I want to find each of the elements in the array by their id and send it with the post request to create a new post. Right now when I create a new post only passes the first index of the array and if I use find() it passes all of the social schemas regardless if it is in the body of the request. I hope this makes sense if it doesn't please let me know. I hope someone can help.
Below is the mongoose schema for the qrcode post also using Joi
const Joi = require("joi");
const mongoose = require("mongoose");
const { themeSchema } = require("./Theme");
const { userSchema } = require("./User");
const { socialSchema } = require("./Social");
const QrCode = mongoose.model(
"QrCode",
new mongoose.Schema({
user: {
type: userSchema,
required: true,
},
name: {
type: String,
maxLength: 255,
required: true,
trim: true,
},
theme: {
type: themeSchema,
required: true,
},
// Social Media Links
social: [
{
type: socialSchema,
required: true,
},
],
})
);
function ValidateQrCode(qrCode) {
const schema = {
userId: Joi.objectId(),
name: Joi.string().max(255).required(),
themeId: Joi.objectId().required(),
socialId: Joi.array().required(),
};
return Joi.validate(qrCode, schema);
}
module.exports.QrCode = QrCode;
module.exports.validate = ValidateQrCode;
this is the post route to create a new qrcode
router.post("/", auth, async (req, res) => {
const { error } = validate(req.body);
if (error) res.status(400).send(error.details[0].message);
const theme = await Theme.findById(req.body.themeId);
if (!theme) return res.status(400).send("Invalid theme.");
const user = await User.findById(req.user._id);
if (!user) return res.status(400).send("Invalid theme.");
const social = await Social.findById(req.body.socialId);
if (!social) return res.status(400).send("Invalid social.");
const qrCode = new QrCode({
user: user,
name: req.body.name,
theme: theme,
social: social,
});
await qrCode.save();
res.send(qrCode);
});
In the body of my Postman request I am inputting the info below
{
"name": "Friends",
"themeId": "60f89e0c659ff827ddcce384",
"socialId": [
"60f89e43659ff827ddcce386",
"60f89e5c659ff827ddcce388"
]
}
To fetch data using ids, you can use below mongodb query simply,
db.collection.find( { _id : { $in : ["1", "2"] } } );
In mongoose,
model.find({
'_id': { $in: [
mongoose.Types.ObjectId('1'),
mongoose.Types.ObjectId('2'),
mongoose.Types.ObjectId('3')
]}
}, function(err, docs){
console.log(docs);
});
Or
await Model.find({ '_id': { $in: ids } });

Mongodb moongose log table

I am interested in how to create log table that writes data in own table, every time user makes some request.
And how to get data like this:
{
_id: ObjectId('4f442120eb03305789000000'),
host: "127.0.0.1",
logname: null,
user: 'frank',
time: ISODate("2000-10-10T20:55:36Z"),
path: "/apache_pb.gif",
request: "GET /apache_pb.gif HTTP/1.0",
status: 200,
response_size: 2326,
referrer: "[http://www.example.com/start.html](http://www.example.com/start.html)",
user_agent: "Mozilla/4.08 [en] (Win98; I ;Nav)"
}
Maybe not all of this data, but atleast who made the request, what type of request, path and time.
I am using nodejs, mongodb, mongoose.
You can write a middleware that logs all the requests, being sent to your server, to the MongoDB Database.
You can easily get the information you are looking for, using these npm packages,
1 - useragent
2 - express-useragent
I solved on this way.
My middlewere
const Log = require("../models/log");
const log = async (req, res, next) => {
try {
let user_id = req.user.id
let firstName = req.user.firstName
let method = req.method
let path = req.path
const log = new Log({ user_id, firstName, method, path });
try {
await log.save()
} catch (e) {
res.status(400).send(e)
}
next();
} catch (e) {
res.status(401).send(e);
}
};
module.exports = log;
Model
const mongoose = require('mongoose')
const logSchema = new mongoose.Schema({
user_id: {
type: String,
},
firstName: {
type: String,
},
method: {
type: String,
},
path: {
type: String,
},
}, {
timestamps: true
})
const Log = mongoose.model('Log', logSchema);
module.exports = Log;
and router
const express = require('express')
const Log = require('../models/log')
const auth = require('../middleware/auth')
const router = new express.Router()
//Create log
router.post('/logs', async (req, res) => {
const log = new Log({
...req.body
})
try {
await log.save()
res.status(201).send(log)
} catch (e) {
res.status(400).send(e)
}
})
//Sort and search
router.get('/logs', auth, async (req, res) => {
const match = {}
const sort = {}
if (req.query.completed) {
match.completed = req.query.completed === 'true'
}
if (req.query.sortBy) {
const parts = req.query.sortBy.split(':')
sort[parts[0]] = parts[1] === 'desc' ? -1 : 1
}
try {
await req.user.populate({
path: 'logs',
match,
options: {
limit: parseInt(req.query.limit),
skip: parseInt(req.query.skip),
sort
}
}).execPopulate()
res.send(req.user.logs)
} catch (e) {
res.status(500).send()
}
})
module.exports = router;

Model not saving to MongoDB using mongoose

I am trying to register a user and save the user to MongoDB. I am creating the user using nodejs readline module. But when I am trying to save it to the mongodb it is not working. Nor does it is returning any error.
Here's the code
const { Admin, validate } = require('../models/admin');
const mongoose = require('mongoose');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const print_error = '\x1b[31m%s\x1b[0m';
const print_success = '\x1b[32m%s\x1b[0m';
function createUser() {
rl.question('Please Enter Username : ', (username) => {
rl.question('Please Enter a Password : ', async(password) => {
const adminObject = {
username,
password
};
const { error } = validate(adminObject);
if (error) {
rl.close()
rl.removeAllListeners()
return console.log(print_error, error.details[0].message);
}
let admin = await new Admin({
username: username,
password: password
});
console.log(admin);
const result = await admin.save(function(err,res){
if(err){
console.log('err',err);
return console.log('err',err);
}
else return console.log('res', res);
}); // save is not working
console.log(print_success, result);
rl.close(process.exit(1)); // close the process after done saving
});
});
}
createUser();
Admin Model
const mongoose = require('mongoose');
const Joi = require('joi');
const Admin = new mongoose.model('Admin', new mongoose.Schema({
username: {
type: String,
required: true,
minlength: 1,
maxlength: 10,
unique: true
},
password: {
type: String,
required: true,
minlength:3,
maxlength: 1024
}
}));
function validateAdmin(admin){
const schema = {
username: Joi.string().min(1).required(),
password: Joi.string().min(3).required()
};
return Joi.validate(admin, schema);
}
exports.Admin = Admin;
exports.validate = validateAdmin;
P.S - I've connected to the mongodb, the connection is successful.
I don't know why you're use async/await and then you use function in your save.
You can change your createUser function with this code below:
function createUser() {
rl.question('Please Enter Username : ', (username) => {
rl.question('Please Enter a Password : ', async(password) => {
const { error } = validate({ username, password });
if (error) {
rl.close()
rl.removeAllListeners()
return console.log(print_error, error.details[0].message);
}
let admin = new Admin({username, password });
try {
const result = await admin.save();
console.log(print_success, result);
rl.close(process.exit(1));
} catch(ex) {
console.log(ex.message);
}
});
});
}
If you've any problem, then let me know in the comment below.
Updated: After test your code, you have no connection in your createUser.js.
I've been add this connection below in above of your createUser() and it's working:
mongoose.connect('mongodb://localhost:27017/url-shortener');
Please, make sure you've add a connection to your mongodb.
I hope it can help you.

Categories

Resources