olvenmage
olvenmage

Reputation: 485

Nodejs export with async/await

Sooo it's another async/await question. I use npm ORM to create an edit function for my models, and this works out perfectly using this code:

module.exports = {
    edit: (model, id, data) => {
        model.get(id, (err, result) => {
            result.save(data)
        })
    }
}

I learned about async and await the other day but when I was experimenting with it, it didn't matter what I do I always got the following error:

"Missing Model.get() callback"

Please help me where I'm going wrong..

Upvotes: 0

Views: 2079

Answers (1)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37383

model.get doesn't return a promise, use getAsync instead :

module.exports = {
    edit: async (model, id, data) => {
        let result = await model.getAsync(id);
        result.save(data)
        return "done";
    }
}

call it like this :

let service = require("./service");

 service.edit(model,id,data).then( data => console.log(data))

Upvotes: 3

Related Questions