I
I
IDONTSUDO2020-02-03 14:32:29
MongoDB
IDONTSUDO, 2020-02-03 14:32:29

Node js can I somehow save synchronously in Mongo?

I do integration with 1-C.

And the problem is that from the 1-C side, there are a lot of repetitions by UUID. I need to filter it somehow. Through ContrAgent.findOne({UUID: agent.UUID }) I thought. But lookup and write happen asynchronously. Is there any way to fix this?

rp(`${process.env.SERVER_1C}`)
        .then(function (xmlData) {
          

            var tObj = parser.getTraversalObj(xmlData, options);

            var jsonObj = parser.convertToJson(tObj, options);
            let atr = jsonObj.UIDs[0].Элемент
            let agent = {}
            for (let i in atr) {
                // console.log(atr[i])
                agent.UUID = atr[i].uid
                agent.full_name = atr[i].naimf
                agent.name = atr[i].naim
                agent.INN = atr[i].inn
                agent.email = atr[i].Email
                agent.payment_account =  atr[i].rs
                
                ContrAgent.findOne({UUID: agent.UUID }).then( data =>{
                    console.log(data)
                    if(data === null){
                    
                    
                    let save_contr_agent =  new ContrAgent(agent)
                    save_contr_agent.save().then(result => {
                        console.log("ok")
                     })
                    }
                })

                console.log(  agent.INN, "  INN  ",agent.name,"  NAME   ", agent.UUID,"   UUID")
            }
            return res.status(200).json({ "OK": "OK" })
        })


UPD:

symbol.iterator

for await() did not work

to make custom _id for documents.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question