Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a new object and saving it to database then find all the objects from database and print it out . but here it's first running the find() method even if i am calling find() method after create(). can anybody explain why is that so?

here is my code :

Cat.create({
    name : "Billy",
    age : 10,
    breed : "Turkish Van"
}, function(err,cat){
    if(err)
        {
            console.log("Oh Error!!");
        }else
            {
                console.log("Great!!");
                console.log(cat); 
                console.log("==========================================");
            }
});

Cat.find({},function(err,cats){
    if(err){
        console.log("Oh Error!!");
    }else {
        console.log("All the cats !!! ");
        console.log(cats);
    }
});


What I have tried:

i tried creting new cat by
var newC=new Cat({
 	name :"Tommy",
 	age : 10,
 	breed: "Persian"
 });

 newC.save(function(err,cat){}


still in console it's running find() method first then create() method. can someone explain why.
Thanks in advance.
Posted
Updated 20-Feb-20 2:48am

1 solution

The create[^] method returns a promise.

That means the method returns before the insert has completed. It returns an object which will be resolved once the operation has completed.

If you want something to run after the insert has completed, you have a number of options:

Callback:
Move the second operation to the callback function:
JavaScript
Cat.create({
    name : "Billy",
    age : 10,
    breed : "Turkish Van"
}, function(err,cat){
    if(err) {
        console.log("Oh Error!!");
    } else {
        console.log("Great!!");
        console.log(cat); 
        console.log("==========================================");
    }
    
    Cat.find({},function(err,cats){
        if(err) {
            console.log("Oh Error!!");
        } else {
            console.log("All the cats !!! ");
            console.log(cats);
        }
    });
});

Use "then":
Invoke the second operation from the .then callback:
JavaScript
Cat.create({
    name : "Billy",
    age : 10,
    breed : "Turkish Van"
}, function(err,cat){
    if(err) {
        console.log("Oh Error!!");
    } else {
        console.log("Great!!");
        console.log(cat); 
        console.log("==========================================");
    }
}).then(function(){
    Cat.find({}, function(err, cats){
        if(err) {
            console.log("Oh Error!!");
        } else {
            console.log("All the cats !!! ");
            console.log(cats);
        }
    });
});

Use async/await:
Change your code to use async / await:
JavaScript
async function makeACat() {
    try {
        const cat = await Cat.create({
            name : "Billy",
            age : 10,
            breed : "Turkish Van"
        });
        
        console.log("Great!!");
        console.log(cat); 
        console.log("==========================================");
    }
    catch (error) {
        console.error("Oh Error!!", error);
    }
    
    try {
        const cats = await Cat.find({});
        console.log("All the cats !!! ");
        console.log(cats);
    }
    catch (error) {
        console.error("Oh Error!!", error);
    }
}
The 80/20 Guide to Async/Await in Node.js | www.thecodebarbarian.com[^]
 
Share this answer
 
Comments
Member 14081431 21-Feb-20 8:48am    
@Richard its showing error as :

}).then(function(){
^

TypeError: Cannot read property 'then' of undefined
Richard Deeming 21-Feb-20 9:00am    
That's odd. Are you using a really old version? It looks like the "promise" support may have been added in v5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900