Entering values as null but still getting error: TypeError: Bind parameters must not contain undefined. To pass SQL NULL specify JS null
See original GitHub issueI’m trying to pass data from my app to mariadb via mysql2. The model method collects the data from the template via req.body.* and then the data presumably gets stored into a new instance of the class Car and that instance is console.logged and is supposed to be passed to the model/class method. When I try and submit the form data, I get the following error:
TypeError: Bind parameters must not contain undefined. To pass SQL NULL specify JS null
Here’s what’s being console.logged (the unneeded properties are ‘javascript nulls’):
Car {
id: null,
model_year: '2008',
make: 'Chevrolet',
model: 'Corvette',
miles: '56000',
color: 'red',
transmission: 'automatic',
layout: 'FR',
engine_config: 'V8',
car_photo_url: null,
car_price: null
}
Here’s the controller method code:
exports.postAddVehicle = (req, res, next) => {
//const id = null;
const model_year = req.body.model_year;
const make = req.body.make;
const model = req.body.model;
const miles = req.body.miles;
const color = req.body.color;
const transmission = req.body.transmission;
const layout = req.body.layout;
const engine_type = req.body.engine_config;
// const car_photo_url = null;
// const car_price = null;
const car = new Car(
null, model_year, make, model,
miles, color, transmission, layout, engine_type,
null, null);
console.log(car);
car
.save()
.then(() => {
res.redirect('/');
})
.catch(err => console.log(err))
}
So there are properties of the class that I don’t want to enter the data for right now so I set those properties to null but apparently mysql2 doesn’t see those properties as javascript nulls?
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (5 by maintainers)
as @dougwilson posted - the issue is that you set
engine_config
in your Car object but later useengine_type
which is undefinedif you can add console.log in mysql2 dependency in your node_modules just before line indicated in
TypeError: Bind parameters must not contain undefined. To pass SQL NULL specify JS null
stack you should be able to see what is actually passed and what missing ( and maybe print query your ORM has generated )