Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:37:07+00:00 2026-06-10T11:37:07+00:00

I have an object passed to a function, and want to save a DB

  • 0

I have an object passed to a function, and want to save a DB entry with those values, with the option to have some defaults.

In practical terms…

I have a schema like this in Mongoose:

var Log = new Schema({
  workspaceId    : { type: String, index: true },
  workspaceName  : { type: String, index: true },
  loginSession   : { type: String, index: true },
  loginToken     : { type: String, index: true },
  logLevel       : { type: Number, enum:[0,1] },
  errorName      : { type: String, index: true },
  message        : { type: String, index: true },
  reqInfo        : { type: String },
  data           : { type: String },
  loggedOn       : { type: Date, index: true },
});
mongoose.model('Log', Log);

To write things on this table, I have something like:

exports.Logger = function(logEntry){
  var Log = mongoose.model("Log"),
      req = logEntry.req;

  log = new Log();

  // Sorts out log.reqInfo
  if ( logEntry.req){
    log.reqInfo = JSON.stringify({
      info   : req.info,
      headers: req.headers,
      method : req.method,
      body   :req.body,
      route  :req.route,
      params: req.params    
    });
  } else {
    logEntry.reqInfo = {};
  }

  // Sorts out all of the other fields with sane defaults.
  // FIXME: improve this code, it's grown into something ugly and repetitive
  log.workspaceId = logEntry.workspaceId ? logEntryworkspaceId. : '';
  log.workspaceName = logEntry.workspaceName ? logEntry.workspaceName : '';
  log.loginSession = logEntry.loginSession ? logEntry.loginSession : '';
  log.loginToken = logEntry.loginToken ? logEntry.loginToken : '';
  log.logLevel = logEntry.logLevel ? logEntry.logLevel : 0;
  log.errorName = logEntry.errorName ? logEntry.errorName : '';
  log.message = logEntry.message ? logEntry.message : '';
  log.data = logEntry.data ? logEntry.data : {};

  // Sorts out log.loggedOn
  log.loggedOn = new Date();
  log.save();
} 

This is absolutely awful code. What’s a better way of writing it, without the repetition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:37:08+00:00Added an answer on June 10, 2026 at 11:37 am

    Something like this might be a bit more elegant:

    Create a dictionary containing the default values

    defaults = {
      workspaceName: 'foo',
      loginSession: 'bar',
      loginToken: 'baz'
    };
    
    console.log(defaults)
    

    Given some values

    values = {
      workspaceName: 'WNAME1',
      loginSession: 'LS1',
      somethingElse: 'qux'
    };
    
    console.log(values)
    

    If values doesn’t contain an entry for any of the specified defaults, add the default value to values

    for(i in defaults){
      if(!values[i]){
        values[i] = defaults[i];
      }
    }
    
    console.log(values)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an [object Object] being passed as an argument through a jQuery function
Hi I have an object rowObject passed into a javascript function. When I inspect
I have a Uri object being passed to a constructor of my class. I
I have a mocked object that is passed as a constructor argument to another
I have an object obj that is passed into a helper method. public static
I think I read somewhere that some modules only have object oriented interfaces (
Let's say I have a function which is being passed a string which originally
I have a populated list, and I want to utilize a custom function to
Sometimes I want to just verify that a function was called rather than have
I want to have an mocked object in all my tests, so I try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.