DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

#8 Finding Records

To search we can use find or findOne method to the model.

MarioChar.findOne({ name: "Mario"}).then(result => {
    ...
})
Enter fullscreen mode Exit fullscreen mode

This code, finds the first match where name==="Mario"
Our finding_test.js file is

const assert = require('assert');
const MarioChar = require('../models/mariochar');

describe('Some demo tests', function(){

    beforeEach(function(done){
        var character = new MarioChar({
            name: "Mario",
        })
        character.save().then(function(){
            assert(character.isNew === false)
            done();
        })
    })

    //Create tests
    it('Finding a record form the database', function(done){
        MarioChar.findOne({ name: "Mario"}).then(result => {
            assert(result.name === "Mario")
            done();
        })
    })
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)