Mongoose multiple save methods save how to get the overall save results?

:projectDao.js
var mongoose = require("mongoose")
const Project = mongoose.model("Project")
exports.createProject = async(data) => {
    data.m.projectModelId=data.m.projectModel.split(",")[0]
    data.m.projectModelName=data.m.projectModel.split(",")[1]
    delete data.m.projectModel
    let project = new Project(data.m)
    let flag1= false
    let flag2=false
    let flag3=false
    await project.save(function (err) {
        if (err) {
            flag1 = false
            return console.error(err)
        }else{
            flag1=true
            return Promise.resolve(flag1)
        }
    })
    console.log(flag1)
    let formModels = new ProjectData(data.formModels)
    await formModels.save(function (err) {
        if (err) {
            flag2 = false
            return console.error(err)
        }else{
            return flag2=true
        }
    })
    let tableModels = new ProjectData(data.tableModels)
    await tableModels.save(function (err) {
        if (err) {
            flag3 = false
            return console.error(err)
        }else{
            return flag3=true
        }
    })
    if(flag1&flag2&&flag3){
        return true
    }
}

controller:

//api
exports.createProject = async (ctx) => {
    const result = projectDao.createProject(ctx.request.body)
    ctx.body = {
        result
    }
}

as above, I want to realize that there are multiple save methods in the same method, one of which is saved in one table and the other in another table. How to get the total results after executing multiple save methods at the same time, because async, await is an asynchronous method, the value of flag obtained outside is actually unchanged.

if the three save are sequentially related, that is, the id obtained after the successful storage of the first save is stored in the second save, how can I write it better?

exports.createProject =(data) => {
    data.m.projectModelId=data.m.projectModel.split(",")[0]
    data.m.projectModelName=data.m.projectModel.split(",")[1]
    delete data.m.projectModel
    let project = new Project(data.m)
    project.save(function (err, info) {
        if (err) {
            console.error(err)
        } else {
            for(let i=0;i<data.formModels.length;iPP){
                let formModel = new ProjectData({projectId:info._id,projectSubmoduleId:data.formModels[i]._id,createUserId:info.createUserId,createUserName:info.createUserName})
                formModel.save(function (err,formData) {
                    if (err) {
                        return console.error(err)
                    }else{
                        return console.log(formData)
                    }
                })
            }
            for(let i=0;i<data.tableModels.length;iPP){
                let tableModel = new ProjectData({projectId:info._id,projectSubmoduleId:data.tableModels[i]._id,createUserId:info.createUserId,createUserName:info.createUserName})
                tableModel.save(function (err,tableData) {
                    if (err) {
                        return console.error(err)
                    }else{
                        return console.log(tableData)
                    }
                })
            }
        }
    })
}
Mar.16,2021

Asynchronous process control is actually the basics of NodeJS and has nothing to do with MongoDB. What you need to understand is how Promise works. If you need to save the above actions one by one, you should use Promise.each . But in fact, several of your operations do not seem to have cause and effect from above, so it should be more efficient to do them at the same time, that is, Promise.all .
your code looks a little strange. I want to know

let project = new Project(data.m)

how is the Project defined here, because

await project.save(function (err) {...}

there are both await and callback , both of which should not occur at the same time.


you can try co, let save1=yield project.save (); so you can get the results of the data after the operation.


it's not a problem with mongodb, it's a problem with the use of Promise. If your three save are not related in sequence, you can use Promise.all:

.
var mongoose = require('mongoose')
const Project = mongoose.model('Project')
exports.createProject = async(data) => {
    data.m.projectModelId=data.m.projectModel.split(',')[0]
    data.m.projectModelName=data.m.projectModel.split(',')[1]
    delete data.m.projectModel
    const p1 =  new Project(data.m).save()
    const p1 =  new ProjectData(data.formModels).save()
    const p1 =  new ProjectData(data.tableModels).save()
    try {
        await Promise.all([p1, p1, p3])
        console.log('save success')
    } catch (e) {
        console.log('save failed')
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b30ffa-2bd4e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b30ffa-2bd4e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?