Leonardo Tomiet
Leonardo Tomiet

Reputation: 11

How can i use insertedCount in mongoDB

When I execute the route "/ new-article" the article is added to the database, but the server does not send a reply to the client. I noticed that this stems from the fact that the constant ris has no insertedCount property and this causes the if condition to never be true, preventing you from executing res.send ()

app.get('/new-article', async (req, res) => {
    const articolo = {
        titolo: 'Titolo articolo 1',
        testo: 'Testo articolo 1',
        autore: 'Leonardo',
        tag: ['node.js', 'javascript', 'mongoDB']
    }
    const ris = await articoliCollection.insertOne(articolo);

    if(ris.insertedCount === 1) {
        res.send('Nuovo articolo inserito correttamente')
    }
});

Upvotes: 1

Views: 382

Answers (1)

Cheng Dicky
Cheng Dicky

Reputation: 518

There is no insertedCount from Collection.insertOne(). Instead, there are acknowledged and insertedId. Doc(InsertOneResult)

insertedCount is from Collection.insertMany() Doc(InsertOneResult)

You can read more from the documentation or use typescript to help you know more about the models.

Upvotes: 1

Related Questions