How to set property to false in a Mongoose Schema if _id doesnt exist

147
March 29, 2018, at 12:40 PM

I have a simple Mongoose schema for creating invoices. It looks like so:

const invoiceSchema = new mongoose.Schema({
  customer: {
    type: mongoose.Schema.ObjectId,
    ref: 'Customer'
  }
})

The thing is, invoices do not always have a customer. In that instance, how do I only set the customer property on the invoice schema if a customer id (which I generate before) exists? In other words, if there is no customer information supplied by the front end, how do I set this customer prop to false?

Answer 1

try this

const invoiceSchema = new mongoose.Schema({
    customer: {
        type: mongoose.Schema.ObjectId,
        ref: 'Customer',
        default: false
    }
})
READ ALSO
Recieving NotFoundError when downloading unnamed files from a Node.js server

Recieving NotFoundError when downloading unnamed files from a Node.js server

I'm downloading files from a Nodejs server in Debian using the following code:

158
Displaying all information in winston logger

Displaying all information in winston logger

when printing an axios error with consolelog there is a lot of information:

252
Heroku Push. Module not found: Error: Can't resolve 'react' in '/tmp/build_XXXXXXXXXXXXXXXX/client/src' [on hold]

Heroku Push. Module not found: Error: Can't resolve 'react' in '/tmp/build_XXXXXXXXXXXXXXXX/client/src' [on hold]

I am trying to deploy to HerokuMy server is delivering my react app but I cant get the heroku deploy to build

144
“ER_CON_COUNT_ERROR: Too many connections” Error with pool connections to mysql from node.js

“ER_CON_COUNT_ERROR: Too many connections” Error with pool connections to mysql from node.js

I have about 20 nodejs files that use the following configuration to access my db:

213