Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when executing low.mixin #72

Closed
balasivagnanam opened this issue Nov 4, 2015 · 15 comments
Closed

error when executing low.mixin #72

balasivagnanam opened this issue Nov 4, 2015 · 15 comments

Comments

@balasivagnanam
Copy link

var low = require('lowdb');
var _contrib = require('underscore-contrib') // For getPath()
var _db = require('underscore.db')

low.mixin(_contrib);
low.mixin(_db);

I saw your example to insert json with inner nodes of the other json. but I get this error when I execute the same code.
Not sure what is missing here.

image

Thanks,

@typicode
Copy link
Owner

typicode commented Nov 4, 2015

Hi @balasivagnanam,

The API has changed, mixin should be called this way now:

low._.mixin(_contrib);

@balasivagnanam
Copy link
Author

both with and without underscore defined..
its still showing error

image

var low = require('lowdb');
var _ = require('underscore');
var _contrib = require('underscore-contrib') // For getPath()
var _db = require('underscore.db')

low._.mixin(_contrib);
low._.mixin(_db);

@typicode
Copy link
Owner

typicode commented Nov 4, 2015

My bad sorry, here is the correct version:

var low = require('lowdb')
var _contrib = require('underscore-contrib')

var db = low('db.json')

db._.mixin(_contrib)

@balasivagnanam
Copy link
Author

is this _ here is underscore definition or its with lowdb...

@balasivagnanam
Copy link
Author

doesn't work that way as well :(

image

var low = require('lowdb');
var _ = require('underscore');
var _contrib = require('underscore-contrib') // For getPath()
var _db = require('underscore.db')
//low._.mixin(_contrib);
//low._.mixin(_db);


 app.get('/', function(req, res) {
   res.sendFile('portal/standards.html' , { root : __dirname});
 });

app.use(express.static('./portal/'));
// Optional any deep link calls should return index.html
app.use('/*', express.static('standards.html'));

app.get('/getCompliance/:filename', function(req, res) {

  console.log("compliance api called");
   console.log(req.params.filename);

   var AdmZip = require('adm-zip');
   var logpath;

   var dbinstance = low('db.json');
   dbinstance._.mixin(require('underscore-contrib'))
   dbinstance._.mixin(require('underscore.db'))

@typicode
Copy link
Owner

typicode commented Nov 4, 2015

Weird, there's a test for mixin here:
https://github.com/typicode/lowdb/blob/v0.10.2/test/index.js#L187

I'll take more time to test later. You're on 0.10.2 right?

@balasivagnanam
Copy link
Author

yes 0.10.2 , even I saw the test when I tried to find the way to implement it. but not sure why it is not working...

here is a gist https://gist.github.com/balasivagnanam/ec1f606f0cbdc0040608

@typicode
Copy link
Owner

typicode commented Nov 4, 2015

Based on your screenshot, it seems like the error comes from line 45. Based on your gist (if it's the same file), it's this line:

var page = dbinstance('results').insert(pagelevel).value(); // Error

You can actually remove .value().
It's only needed if theres a chain() before.

@balasivagnanam
Copy link
Author

I am passing dbinstance and page to another module which is doing insert as a subset inside this..

dbinstance('results').get(page.id).getPath('components').insert({component :'header',
                                                    condition : "Standard MOM Header should be present",
                                                    result : "Fail" ,
                                                    remarks: "Missing Standard Header"
                                                  });

you can check the same gist..

@balasivagnanam
Copy link
Author

and the error is at getPath

TypeError: undefined is not a function
at Object.module.exports.getCheckHeader (C:\mytests\momtest\standard-components.js:51:46)
at Object.module.exports.getComponentCheckResults (C:\mytests\momtest\checkermain.js:136:28)
at C:\mytests\momtest\server.js:67:43
at Array.forEach (native)
at C:\mytests\momtest\server.js:53:16
at Layer.handle as handle_request
at next (C:\mytests\momtest\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\mytests\momtest\node_modules\express\lib\router\route.js:112:3)
at Layer.handle as handle_request
at C:\mytests\momtest\node_modules\express\lib\router\index.js:277:22

@typicode
Copy link
Owner

typicode commented Nov 4, 2015

It's actually because you're chaining lodash and other methods without chain(). The following code shouldn't throw an error:

dbinstance('results')
  .chain()
  .getById(page.id) // I think you want to get by id here
  .getPath('components')
  .insert({component :'header',
    condition : "Standard MOM Header should be present",
    result : "Fail" ,
    remarks: "Missing Standard Header"
  })
  .value()

@balasivagnanam
Copy link
Author

there is no error after doing chain(), but with get(page.id) there is no insert at inside level happening, when I change to getById I start to get undefined is not a function at this location.

updated gist

@balasivagnanam
Copy link
Author

woww... it works :)

{
  "results": [
    {
      "name": "page-name",
      "description": "paris",
      "components": [
        {
          "component": "header",
          "condition": "Standard MOM Header should have only the defined elements",
          "result": "Fail",
          "remarks": "Standard MOM Header has more elements than the standard",
          "id": "7eb0b3d9-0d6c-49be-a288-f1981ca4718d"
        }
      ],
      "id": "a88bb8f2-de12-4cd9-8d26-5ccced79ccc3"
    }
  ]
}

@balasivagnanam
Copy link
Author

thanks a lot :) your help is highly appreciated :)

@typicode
Copy link
Owner

typicode commented Nov 5, 2015

You're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants