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

Insert doesn't take place immediately #684

Open
teoring opened this issue Aug 17, 2021 · 2 comments
Open

Insert doesn't take place immediately #684

teoring opened this issue Aug 17, 2021 · 2 comments

Comments

@teoring
Copy link

teoring commented Aug 17, 2021

I was trying to find any existing issue and went through the documentation but didn't manage to figure out what is wrong.
In my case I am inserting elements in the database in the callback of the find function that is called in the loop. Here is the code snippet:


for( var i = 0; i < houses.addresses.length; i++ ) {
     let house = {
         address: addresses[i]
     };
     
    await db.houses.find( { address: house.address }, async function( err, docs ) {
        if( docs.length != 0 ) {
            return; 
        }
        
        await db.houses.insert( house, function ( error, newDoc ) {
            if( error ) {
                logger.error('Cant save the document: ' + JSON.stringify( doc ) + '. Caused by: ' + error );
            }
        });
        

So I am trying to find an entry, if entry doesn't exist I insert the entry. But for some reason I end up inserting many entries:

{"address":"Vaartsestraat 0 ong","_id":"x5BtMrYmkiv2tOhy"}
{"address":"Vaartsestraat 0 ong","_id":"hms08wtX7yAEFpX3"}
{"address":"Vaartsestraat 0 ong","_id":"OjW99USIFU3adzoo"}
.....

Any idea why this might be happening?

@teoring
Copy link
Author

teoring commented Aug 18, 2021

I am using "nedb": "^1.8.0" version from npm.

@program-spiritual
Copy link

you should use Promise API to wrap callback function

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