How to DELETE multiple records in postgresql with NodeJS? - javascript

i have created a controller that can delete my car recors, but i do not know how to DELETE MULTIPLE RECORDS with postgresql. I hope anybody can help me and give me some ideas in order to make this. Below it is my controller but it only delete one, now i wanna make delete multiple records please help me
// delete a car
export async function deleteCar(req, res) {
try {
const car = await Car.findByIdAndDelete(req.params.car_id);
if (!car) {
return res.status(404).send();
}
res.send(car);
} catch (error) {
return res.status(500).json({
success: false,
message: "Server error. Please try again.",
error: error.message,
});
}
}
Model
import Sequelize from 'sequelize';
export const Car = sequelize.define("car", {
car_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement:true,
},
name: {
type: Sequelize.STRING,
unique: true,
required: [true, 'Car name required'],
},
color: {
type: Sequelize.STRING,
unique: true,
required: [true, 'Car color required'],
},
brand: {
type: Sequelize.STRING,
unique: true,
required: [true, 'Car brand required'],
},
}, { sequelize, freezeTableName: true}
)
export default Car;
Routes
router.delete("/cars/:carId", deleteCar);

If you want to delete multiple record in Sequelize You can use this code instead:
// delete a car
export async function deleteCar(req, res) {
try {
//multipleIds = req.params.car_ids;
//you should send array of ids you want to delete in req.params.car_ids
//and delete the next line
let multipleIds = [100, 101, 102, 103]; // You can delete this line later
const car = await Car.destroy({ where: { id: multipleIds }});
//this delete cars with id 100, 101, 102, 103
if (!car) {
return res.status(404).send();
}
res.send(car);
} catch (error) {
return res.status(500).json({
success: false,
message: "Server error. Please try again.",
error: error.message,
});
}
}
After you find out how it works You can fill multipleIds variable dynamically

Related

MongoServerError: E11000 duplicate key error collection: with mongoose save()

i have this model with username and email set uniqe to false
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
minlength: 3,
maxlength: 20,
unique: false,
},
email: {
type: String,
required: true,
minlength: 5,
maxlength: 64,
lowercase: true,
unique: false,
},
mailboxLink: {
type: String,
required: true,
unique: true,
default: nanoid(),
},
createdat: { type: String, default: dateJakarta },
});
and 1 user in my mongodb database
{"_id":{"$oid":"622eec9de7f66d1d633061e7"},"username":"jhon","email":"email#gmail.com","mailboxLink":"mfdYTDK","createdat":"2022-03-14 14:19:01","__v":0}
but when i'm trying to register the same username and email
userRouter.post("/register", async (request, response) => {
const newUser = new userSchema(request.body); // body: {username, email}
await newUser.save((err, user) => {
if (err) {
console.log(err);
} else {
const token = newUser.generateAuthToken();
response.status(201).json({
token,
user,
message: "Register successfully",
});
}
});
});
i got this error from console.log(err);
MongoServerError: E11000 duplicate key error collection: PigeonProjekt.users index: email_1 dup key: { email: "email#gmail.com" }
index: 0,
code: 11000,
keyPattern: { email: 1 },
keyValue: { email: 'email#gmail.com' }
could someone help me solve this problem.
If you can, try dropping the database or the collection itself then test again. If you first set the email field to unique: true and then inserted the user that you have in the database, but later changed the unique value of email to false in your schema, this will not update the collection itself. In short, rules in the database for email unique: true still apply and you need to remove them.

Sequelize validation doesn't work on PUT method

I'm pretty new to Sequelize. Here's a model that I have. The validation seems to be working fine on POST method, but it doesn't work on PUT method. I'm not sure what I'm missing here. I'd appreciate all the help guys.
const Sequelize = require('sequelize');
module.exports = (sequelize) => {
class Course extends Sequelize.Model {}
Course.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Please provide a value for "title"'
},
notEmpty: {
msg: 'Please provide a value for "title"'
}
}
},
description: {
type: Sequelize.TEXT,
allowNull: false,
validate: {
notNull: {
msg: 'Please provide a value for "description"'
},
notEmpty: {
msg: 'Please provide a value for "description"'
}
}
}
}, { sequelize });
Course.associate = (models) => {
Course.belongsTo(models.User, {
foreignKey: {
fieldName: 'userId',
allowNull: false
}
})
};
return Course
}
Here's my code on main route.
router.put('/courses/:id', asyncHandler(async(req, res) => {
try {
const course = await Course.findByPk(req.params.id)
await course.update(req.body);
res.status(204).end();
} catch (error) {
console.error(error);
}
}));

Model methods are not working in sails js

I am using sails v0.12 , I have different models in my MySql relational database but the area of concern lies in these 2 models which are
1. User
2. Appointment
I want to add apptCustomer and apptProvider data into appointment model
but the value is NULL in the database
My User model is :
module.exports = {
attributes: {
name: {
type: "string",
},
email: {
type: "email",
required: true,
unique: true
},
contact_no:{
type: "int",
maxLength: 15,
//required: true,
},
address: {
type:"longtext",
//required: true,
},
userId:{
type: "string",
primaryKey: true,
unique:true
},
gtoken:{
type: "string"
},
provider:{
type:"string"
},
cancel:{
type: "boolean",
// required: true
},
business_name:{
type:"string",
unique:true
},
business_category:{
type:"string"
},
roles:{ // Many to Many = User <-> User-role <-> Role
collection:'Role',
through:'userrole',
},
services:{
collection:'Service',
through:'serviceprovider', // Many to Many = User (provider) <-> Service-provider <-> Service
},
schedules:{ // One to One = User (provider) - Schedule
collection:'Schedule',
via:'owner',
},
providerAppointments:{ // One to Many = User(customer) - multiple Appointments
collection:'Appointment',
via:'appProvider',
},
customerAppointments:{
collection:'Appointment', // One to Many = User(provider) - multiple Appointments
via:'appCustomer'
}
}
};
And my Appointment Model is
module.exports = {
attributes: {
appointment_id:{
type:'int',
primaryKey: true,
autoIncrement:true
},
appointmentDate: {
type: 'datetime',
required: true,
},
start_time: {
type: 'string',
required: true,
},
end_time: {
type: 'string',
},
status: {
type: 'string',
enum: ['booked', 'canceled']
},
appProvider: {
model: 'user',
},
appCustomer:{
model: 'user',
},
serviceAppointment: {
model: 'service',
}
}
};
And my Model Methods are as follows
Appointment.findOrCreate({appointmentDate:req.body.values.appointmentDate, start_time:req.body.values.start_time, end_time:req.body.end_time, status: status},{appointmentDate:req.body.values.appointmentDate, start_time:req.body.values.start_time, end_time:req.body.end_time, status: status})
.exec(function apptCreated(err,appt){
if(err) { sails.log('err',err)}
Service.findOne({ service_id : req.body.values.selected_service})
.exec(function(err,service){
service.serviceAppointments.add(appt);
service.save(function(err,result){
if(err) { sails.log(err)}
})
}),
User.find({userId: req.body.businessId})
.populate('roles')
.exec(function(err,provider){
_.map( provider.roles, role => { // Problem lies here .. this method is not working
if(role.role_id==1){
provider.providerAppointments.add(appt);
provider.save(function(err, result){
if(err) { sails.log(err)}
sails.log('appointment added to provider')
})
}
})
}),
//Appointment adding to Customer
User.find({userId: req.body.customerId})
.populate('roles')
.exec(function(err,customer){
_.map( customer.roles, role => { // Problem lies here... this method is not working
if(role.role_id==2){
customer.customerAppointments.add(appt)
customer.save(function(err, result){
if(err) { sails.log(err)}
sails.log('appointment added to customer')
})
}
})
}),
// Adding contact to customer
User.update({userId: req.body.customerId},{ contact_no: req.body.values.contact_no}) // this method works fine
.exec(function userUpdated(err, user){
if(err) { return sails.log(err)}
sails.log('contact number updated',user);
})
})
As far as I can see, at the moment that you call provider.providerAppointments.add, provider.providerAppointments is still just a regular property - possibly an array of ids.
I think you need to add .populate('providerAppointments') to your User.find... if you do that, then provider.providerAppointments should have a .add method that works the way you expect.
Of course, if this is the source of error, I would have expected a pretty clear error message, like provider.providerAppointments.add is not a function or some such. But try adding the populate, see if it fixes your problem.

Sequelize create method is ignoring validation rules

I am building a small REST API with node, hapi and sequelize. When attempting to create a new user without the required data, I expect validation errors. Instead, I receive database errors.
The model:
'use strict';
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User',
{
email: {
type: DataTypes.STRING,
validate: {notEmpty: true, isEmail: true}
},
password: {
type: DataTypes.STRING,
validate: {notEmpty: true, min: 6}
},
name: {
type: DataTypes.STRING,
validate: {notEmpty: true}
},
is_active: {
type: DataTypes.BOOLEAN
},
api_key: {
type: DataTypes.STRING,
validate: {notEmpty: true}
}
},
{
classMethods: {
associate: function(models) {
// associations can be defined here
User.hasMany(models.Keeper);
}
},
timestamps: true,
createdAt: 'created',
updatedAt: 'modified',
deletedAt: false
}
);
return User;
};
The code:
exports.users = {
/* ... */
create: function(req, reply) {
models.User.create(req.payload)
.then(function(err, user) {
/* ... */
})
.catch(function(err) {
console.log(err);
});
reply('test');
},
/* ... */
};
The error:
{ [SequelizeDatabaseError: ER_NO_DEFAULT_FOR_FIELD: Field 'password' doesn't have a default value]
name: 'SequelizeDatabaseError',
message: 'ER_NO_DEFAULT_FOR_FIELD: Field \'password\' doesn\'t have a default value',
parent:
{ [Error: ER_NO_DEFAULT_FOR_FIELD: Field 'password' doesn't have a default value]
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlState: 'HY000',
index: 0,
sql: 'INSERT INTO `Users` (`id`,`email`,`modified`,`created`) VALUES (DEFAULT,\'email#email.com\',\'2015-04-24 04:35:49\',\'2015-04-24 04:35:49\');' },
original:
{ [Error: ER_NO_DEFAULT_FOR_FIELD: Field 'password' doesn't have a default value]
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlState: 'HY000',
index: 0,
sql: 'INSERT INTO `Users` (`id`,`email`,`modified`,`created`) VALUES (DEFAULT,\'email#email.com\',\'2015-04-24 04:35:49\',\'2015-04-24 04:35:49\');' },
sql: 'INSERT INTO `Users` (`id`,`email`,`modified`,`created`) VALUES (DEFAULT,\'email#email.com\',\'2015-04-24 04:35:49\',\'2015-04-24 04:35:49\');' }
I'm expecting a validation error for not passing a password, instead I'm getting a SQL error. Halp!
Validation will only happen if a value is already set for the field. So if the password field is null, the validation won't run.
To fix this, set allowNull on the column:
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {notEmpty: true, min: 6}
},
The .catch() function you are using seems to be used in 'Database synchronization' according to the documentation. Since you already have the err parameter and the callback in your 'then' function, place your console log statement there. Like so:
/* ... */
create: function(req, reply) {
models.User.create(req.payload)
.then(function(err, user) {
if (err){
console.log(err);
}
})
reply('test');
},
/* ... */
The 'then' function is already 'catching' the validation errors if any.

id: null issue in sailsJS when create an User

I suspect my issue is something silly, but I can't find the error. I'm using mongodb to persist user data. Every seems to work fine but when I look the database I have id: null in every user record.
I don't want that id: null, I already have _id field.
$sails --version
0.11.0
My userController:
module.exports = {
create: function(req, res) {
User.create(req.params.all(), function userCreated(err, user) {
if(err) res.json(401, err);
res.json(200, user);
})
}
}
My user model:
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
email: true,
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
maxLength: 15,
columnName: 'encrypted_password',
required: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
},
beforeCreate: function(values, next) {
require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
if(err) console.log(err);
values.password = encryptedPassword;
next();
});
}
};
When I create a user from url like http://localhost:1337/user/create?name=theUser&email=user#mail.com&password=123456&role=admin everythink seems to be fine but in my mongodb I see this: (id: null)
{
name: "theUser",
email: "user#mail.com",
role: "admin",
id: null,
createdAt: ISODate("2015-04-27T18:34:42.678Z"),
updatedAt: ISODate("2015-04-27T18:34:42.678Z"),
encrypted_password: "$2a$10$iNt/OR8XhjijqRjkpoNW/eR70HTSDgVJ2WmNppqab79rZt213aywm",
_id: ObjectId("553e81429255e51f419a8ffc")
}
I'd tried with autoPK: false but nothing happens.
Thanks in advance.
As stated in waterline docs,
By default an attribute named id will be automatically added to your
model which will contain an auto-incrementing number unique to each
record. This will be your model's primary key and will be indexed when
available. You can override this if you would like to define your own
primary key factory or attribute.
primaryKey
Will set the primary key of the record. This should be used when autoPK is set to false.
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
}
}

Categories

Resources