2

I am trying to get a good understanding of how to work with promises, mostly because I want to query data and then use that data to query other data

I have this Schema :

var serviceSchema = new Schema({
name: String,
});

And this method on it :

serviceSchema.statics.getIdByName = function getIdByName (serviceName) {

this.findOne({name :serviceName }).exec(function(err, service){
    if(err) console.log(err)

    return service._id

})
}

In my code I would like to do something like :

var service_id = Service.getIdByName("facebook")
otherSchema.findOne({service : service_id}).exec(...)

but service_id is a promise so the query isn't right, I don't want to be in callback hell and calling models inside of callbacks etc I have tried async with something like

async.series([getVariables], function(err, results){
otherSchema.findOne({service : service_id}).exec(...)})

Where getVariables is :

function getVariables(callback1) {
if(service_id!=undefined) {
    callback1(serviceID)
}}

Any help on how to achieve this is more than welcome! thanks a lot

2 Answers 2

2

try this exec() returns promise.

serviceSchema.statics.getIdByName = function getIdByName(serviceName) {
    return this.findOne({
        name: serviceName
    }).exec();
}

Call getIdByName function

Service.getIdByName("facebook").then((data) => {
    console.log(data)
})
Sign up to request clarification or add additional context in comments.

10 Comments

thanks a lot for your answer, this doesnt solve my problem because getIdByName is now only a findOne function and I can access the service's id only inside the 'then' where i would like to access it outside, stored in a variable i can pass as parameter to an other query does that mean i can only do that inside the 'then' ? isnt that callback hell, because in fact i have to get many ids before doing the query
@mtths It'll not create callback hell because you can create the chain of promises, not callback hell.
what did you try?
heres a more readable version : Service.findOne({name : 'JollyClick'}).then(function(service){ var service_id = service._id return Data.findOne({name : "Objectifs"}).exec() }) .then(function(data){ var objectifsID = data._id return Data.findOne({name : 'Personnalité'}).exec() }). then(function(data2){ var personnaliteID = data2._id DataUse.create({service : service_id, user : service_id, data : dataTypesIds}, function(err, datause){ }) cant access service_id when i want to create my document
declare service_id outside .then and assign value inside .then. var service_id; Service.findOne({ name: 'JollyClick' }).then(function (service) { service_id; = service._id return Data.findOne({ name: "Objectifs" }).exec(); })
|
0

The answer to this is actually using async/await and and do var x = await Model.findOne().exec()

or any function that returns a promise, then you can use x anywhere in the code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.