Nodejs REST API function throws error "Callback is not a function" - node.js

I am writing a node app's REST API using Sqlite3. The app will have accounts, and the user should be able to create and update one. My code for creating and fetching accounts works as intended, but my update function throws the error: "TypeError: callback is not a function"
The backend is split in two files; db.js – where I set up the database and create the base function for get/post/put/delete, and app.js – where I call the function from db and perform validation checks.
when i run the function in postman, i get error code 500. in vscode, the terminal reads:
Project/rbrneck/rbrneck-backend/db.js:124
callback([], updatedAccount)
^
TypeError: callback is not a function
at Statement.db.run
code:
//in db.js
exports.updateAccountById = (id, updatedAccount, callback) => {
const query = 'UPDATE accounts SET username = ?, password = ? WHERE id = ?'
const values = [
id,
updatedAccount.username,
updatedAccount.password
]
db.run(query, values, (error) => {
if(error) {
if(error.message == "SQLITE_CONSTRAINT: UNIQUE constraint failed: accounts.username") { //username taken
callback(['usernameTaken'])
} else {
callback(['databaseError'])
}
} else {
//const accountUpdated = (this.changes == 1)
callback([], updatedAccount) //HERE IS THE CALLBACK THE ERROR IS REFERRING TO
}
})
}
// in app.js:
app.put('/accounts/:id', (req, res, next) => {
const id = req.params.id
const updatedAccount = req.body
//errors and validation
//type of input
if(typeof(updatedAccount.username) !== 'string' && typeof(updatedAccount.password) !== 'string') {
res.status(422).json({
message: 'Unprocessable Entry'
}).end()
return
}
//does the account exist?
db.getAccountById(id, (errors, oldAccount) => {
if(errors.length > 0) {
res.status(500).json({
message: 'Internal Server Error'
}).end()
return
} else if (!oldAccount) {
res.status(404).end()
return
}
})
//validation:
const validationErrors = []
if(updatedAccount.username.length < USERNAME_MIN_LENGTH) {
validationErrors.push('Username too short')
} else if (updatedAccount.username.length > USERNAME_MAX_LENGTH) {
validationErrors.push('Username too long')
}
if(updatedAccount.password.length < PASSWORD_MIN_LENGTH) {
validationErrors.push('Password too short')
} else if (updatedAccount.password.length > PASSWORD_MAX_LENGTH) {
validationErrors.push('Password too long')
}
if(validationErrors.length > 0) {
res.status(400).json(validationErrors).end()
return
}
db.updateAccountById(updatedAccount, (errors, userId) => {
if(errors.length == 0) {
res.setHeader('Location', '/accounts/' + userId)
res.status(201).end()
} else if (errors.includes('usernameTaken')) {
res.status(400).json(errors).end()
} else {
res.status(500).end()
}
})
})

Related

use async - await with socket.io nodejs

I'm developing a web application using nodejs socket.io and angular 9. In my backend code I have written sockets in socket.connect.service.
Follows is a socket I'm using
socket.on('request-to-sit-on-the-table', async function (data, callback) { //Previously Register
let table = persistence.getTable(tableToken);
if (typeof table === 'undefined') {
let errMsg = 'This table does not exists or already closed.'
callback(prepareResponse({}, errMsg, new Error(errMsg)));
return;
}
//TODO: Get the displayName from the token.
let guest = await guestUserService.getGuestUserByPlayerToken(JSON.parse(data.userToken));***//Here is the issue***
// let displayName = 'DisplayName-' + guest;
let displayName = 'DisplayName-' + Math.random();
//TODO: Check whether the seat is available
// If the new screen name is not an empty string
let isPlayerInCurrentTable = persistence.isPlayerInCurrentTable(tableToken, userToken);
if (displayName && !isPlayerInCurrentTable) {
var nameExists = false;
let currentPlayersTokenArr = persistence.getTableObjectPlayersToken(table)
for (var token in currentPlayersTokenArr) {
let gamePlayer = persistence.getPlayerPlayer(currentPlayersTokenArr[token])
if (typeof gamePlayer !== "undefined" && gamePlayer.public.name === displayName) {
nameExists = true;
break;
}
}
if (!nameExists) {
//Emit event to inform the admin for requesting to sit on the table.
let ownerToken = persistence.getTableObjectOwnerToken(table);
let ownerSocket = persistence.getPlayerSocket(ownerToken);
ownerSocket.emit('requested-to-sit', {
seat: data.seat,
secondaryUserToken: userToken,
displayName,
numberOfChips: envConfig.defaultNumberOfChips
});
callback(prepareResponse({userToken}, 'Player connected successfully.'));
} else {
callback(prepareResponse({}, 'This name is already taken'));
}
} else {
callback(prepareResponse({}, 'This user has already joined to a game. Try clear caching'));
}
});
In my code I'm getting data from another code in guest.user.service. But I get undefined to the value of "guest"
Follows are the methods I have used in guest.user.service
exports.findById = (id) => {
return new Promise(function(resolve, reject) {
guestUserModel.findById(id, (err, data) =>{
if(err){
reject(err);
} else {
resolve(data);
}
});
});
};
exports.getGuestUserByPlayerToken = (playerToken) => {
var player = playerService.findOne({ token: playerToken })
.then(function (data) {
return self.findById(data.guestUser._id.toString());
})
.then(function (guestUser) {
return guestUser.displayName;
})
.catch(function (err) {
throw new Error(err);
})
};
Although I get my displayName for the return value It is not passed to the "guest" in my socket.Is there any syntax issue to get data as I'm using promises.please help
exports.getGuestUserByPlayerToken = async playerToken => {
try {
let player = await playerService.findOne({token:playerToken});
return playerService.findById(player.guestUser._id)
} catch(error) {
console.log(error);
return null;
}
};
This is just handle error on awaited promise not returned one. You need to handle that in caller side.

pass mysql value from node js to angular

Hi i have this post function to call create function which will insert to database
router.post('/', async (req, res, next) =>
{
const body = req.body;
console.log("tu sam todo_task",req.body);
try
{
const todo_task = await TodoTaskService.create(body);
console.log(todo_task, "function")
// created the todo_task!
return res.status(201).json({ todo_task: todo_task });
}
catch(err)
{
}
});
Then in second snippet i have this create function in service class where i want to return result from mysql query
class TodoTaskService
{
static create(data)
{
console.log("tu sam service todo_task create");
var vres = todoTaskValidator.validate(data, todo_taskVSchema);
/* validation failed */
if(!(vres === true))
{
let errors = {}, item;
for(const index in vres)
{
item = vres[index];
errors[item.field] = item.message;
}
throw {
name: "ValidationError",
message: errors
};
}
console.log("tu sam service todo_task validation passed");
let todo_task = new TodoTaskModel(data.todo_task_name, data.todo_task_complete_f);
let connection = mysql.createConnection(dev);
// insert statment
let sql = `INSERT INTO todo_task (todo_task_name,todo_task_complete_f) VALUES (?);`
let values = [data.todo_task_name, data.todo_task_complete_f];
// execute the insert statment
connection.query(sql, [values], (err, results, fields) => {
if (err) {
global.query = "Database error";
console.log(err.message + "zasto");
return err
}
else{
// get inserted rows
global.query = "OK";
todo_task.todo_task_id = results.insertId
console.log("rows inserted",todo_task);
return todo_task;
}
});
}
}
How can i pass return todo_task from connection.query back to the first post function and send it to angular?
I've found a solution on my own, don't know how if it's the best practice but here is my solution, I'm using callback to pass the value
router.post('/', async (req, res, next) =>
{
const body = req.body;
try
{
return await TodoTaskService.create(body,function (result){
console.log(result, "function result")
todo_task = result;
return res.status(201).json({ todo_task: todo_task });
});
// created the todo_task!
//return res.status(201).json({ todo_task: todo_task });
}
returning callback with result
class TodoTaskService
{
static create(data,callback)
{
var vres = todoTaskValidator.validate(data, todo_taskVSchema);
/* validation failed */
if(!(vres === true))
{
let errors = {}, item;
for(const index in vres)
{
item = vres[index];
errors[item.field] = item.message;
}
throw {
name: "ValidationError",
message: errors
};
}
console.log("tu sam service todo_task validation passed");
let todo_task = new TodoTaskModel(data.todo_task_name, data.todo_task_complete_f);
/* todo_task.uid = 'c' + counter++;
todo_tasks[todo_task.uid] = todo_task; */
let connection = mysql.createConnection(dev);
// insert statment
let sql = `INSERT INTO todo_task (todo_task_name,todo_task_complete_f) VALUES (?);`
let values = [data.todo_task_name, data.todo_task_complete_f];
// execute the insert statment
connection.query(sql, [values], (err, results, fields) => {
if (err) {
global.query = "Database error";
console.log(err.message + "zasto");
return err
}
else{
// get inserted rows
global.query = "OK";
todo_task.todo_task_id = results.insertId
console.log("rows inserted",todo_task);
return callback(todo_task);
}
});
}

How to consume a RESTful API in Node.js

I'm new to Node.js and I'm creating a simple pagination page. The REST API works fine, but consuming it has left me in limbo.
Here is the REST API (other parts have been taken out for brevity)
const data = req.query.pageNo;
const pageNo =
(typeof data === 'undefined' || data < 1) ? 1 : parseInt(req.query.pageNo);
let query = {};
const total = 10;
query.skip = (total * pageNo) - total;
query.limit = total;
try {
const totalCount = await Users.countDocuments();
const pageTotal = Math.ceil(totalCount / total);
const users = await Users.find({}, {}, query);
return res.status(200).json(users);
} catch (error) {
console.log('Error ', error);
return res.status(400).send(error)
};
};
When I return the json with just the 'users' object, like so return res.status(200).json(users); the page renders correctly, but when I pass in other objects like what I have in the code, it fails. This is how I'm consuming the API:
const renderHomepage = (req, res, responseBody) => {
let message = null;
if (!(responseBody instanceof Array)) {
message = 'API lookup error';
responseBody = [];
} else {
if (!responseBody.length) {
message = 'No users found nearby';
}
}
res.render('users-list', {
title: 'Home Page',
users: responseBody,
message: message
});
}
const homelist = (req, res) => {
const path = '/api/users';
const requestOptions = {
url: `${apiOptions.server}${path}`,
method: 'GET',
json: true,
};
request(
requestOptions,
(err, {statusCode}, body) => {
if (err) {
console.log('Ther was an error ', err);
} else if (statusCode === 200 && body.length) {
renderHomepage(req, res, body);
} else if (statusCode !== 200 && !body.length) {
console.log('error ',statusCode);
}
}
);
}
I've searched extensively on both here and other resources but none of the solutions quite answers my question. I hope someone could be of help

Delete and EDIT API's using expressJS

I created DELETE API and in the callback function I have written .remove function to delete a product if the id I have given is present. But when I am giving incorrect input, the result is receiving some object which shouldn't be the case. As there is no data with the Id i have given it should be null or undefined.
Can someone help on this?
Route URL-
http://localhost:3000/api/v1/products/:productId/delete
Code -
let deleteProduct = (req, res) => {
if (check.isEmpty(req.params.productId)) {
console.log('productId should be passed')
let apiResponse = response.generate(true, 'productId is missing', 403, null)
res.send(apiResponse)
} else {
productModel.remove({ 'productId': req.params.productId }, (err, result) => {
if (err) {
console.log('Error Occured.')
logger.error(`Error Occured : ${err}`, 'Database', 10)
let apiResponse = response.generate(true, 'Error Occured.', 500, null)
res.send(apiResponse)
} else if (check.isEmpty(result)) {
console.log('Product Not Found.')
let apiResponse = response.generate(true, 'Product Not Found.', 404, null)
res.send(apiResponse)
} else {
console.log('Product Deletion Success')
let apiResponse = response.generate(false, 'Product Deleted Successfully', 200, result)
res.send(apiResponse)
}
})
}
}
let isEmpty = (value) => {
if (value === null || value === undefined || trim(value) === '' || value.length === 0) {
return true;
} else {
return false;
}
}
When I give incorrect productId, Ideally it should give 'Product Not found' but the output is coming as Product Deleted Successfully.
The result will contain an object no matter if it was successful or not, it will never be empty in this case. Please read the documentation here.
check.isEmpty(result) will always be false.

Sequelize.js: findById() not found the data

I want to verify incoming token and I try to find user by Id:
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
db.users.findById(decodeToken.id).then(function (foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}), function (error) {
return error;
}
But I get "return false". I view foundUser variable for debuggindg, and it has message
'Reference Error: foundUser is nor defined'
In console I can see query:
Executing (default): SELECT "id", "username", "email", "password",
"createdAt", "updatedAt" FROM "users" AS "users" WHERE "users"."id" =
2;
And I have the user with id=2 in a db.
Why it doesn't work?
Addition:
I tried MWY's modified example:
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
findUserById().then(function(foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}), function (error) {
return error;
}
function findUserById() {
return db.users.findById(decodeToken.id).then(function (foundUser) {
return foundUser;
}), function (error) {
return error;
}
}
}
And get error:
TypeError: findUserById(...).then is not a function
Be sure to remember asynchronously
Because asynchronously! you will get false first, then get the result!
So you can write like this, In ty.js file
module.exports = function (req) {
var decodeToken = jwt.decode(req.cookies.token, JwtOptions.secretOrKey);
return db.users.findById(decodeToken.id).then(function (foundUser) {
//It's example checking for self-learning
if (foundUser.username == req.cookies.username) {
return foundUser;
}
//Or more logic for token authentication
}).catch(function (err) {
return err;
})
};
In tu.js file:
var user = require('./ty');
user().then(function (result) {
//search findById result
console.log(result)
});

Resources