Home > Software design >  insert into NedB boolean value without quotation marks
insert into NedB boolean value without quotation marks

Time:05-07

I have a NedB database which I'm trying to insert some new data into that I retrieve from HTML form, this is my function to insert new.

addEntry(name,desc,ingred,allergy,cat,aval,price){
        var entry = {
            Name: name,
            Description: desc,
            Ingerdients: ingred.split(','),
            Allergy: allergy.split(','),
            Category: cat,
            Availability: aval,
            price: price
        }
        this.db.insert(entry, function(err,doc) {
            if(err)
            {
                console.log("error inserting document",subject);
            }
            else{
                console.log('Meal Added Successfully');
            }
        })
    }

My problem is that the Availability is a boolean value, yet it's added in the database with quotation marks. For example: 'true' instead of true and 'false' instead of false, how can I fix this?

CodePudding user response:

Assuming that the parameter aval is a string, you could convert it to a boolean:

var entry = {
  Name: name,
  Description: desc,
  Ingerdients: ingred.split(','),
  Allergy: allergy.split(','),
  Category: cat,
  Availability: aval === "true",
  price: price
}

But I am surprised that the database accepts a string value where it should expect a boolean. Doesn't it give you an error message?

  • Related