Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何为数据保存多个循环?_Mongodb_Api_Express_Mongoose Schema - Fatal编程技术网

Mongodb 如何为数据保存多个循环?

Mongodb 如何为数据保存多个循环?,mongodb,api,express,mongoose-schema,Mongodb,Api,Express,Mongoose Schema,在Post请求中,我希望提取objectId的数据并将其保存在数据库中。将有2或3个objectId,我将在数组中获取它。从该数组中,我可以使用单独的“for循环”提取数据,但最后我无法保存数据。 我想知道如何将数据从一个阵列移动到另一个阵列或直接保存数据 模式:-- 邮政编码:-- router.post('/',异步(req,res)=>{ 让ids=req.body; 设catId=ids.categoryId; console.log(“catId”,catId) console.log

在Post请求中,我希望提取objectId的数据并将其保存在数据库中。将有2或3个objectId,我将在数组中获取它。从该数组中,我可以使用单独的“for循环”提取数据,但最后我无法保存数据。 我想知道如何将数据从一个阵列移动到另一个阵列或直接保存数据

模式:--

邮政编码:--

router.post('/',异步(req,res)=>{
让ids=req.body;
设catId=ids.categoryId;
console.log(“catId”,catId)
console.log(“catId.length”,catId.length)
设subcategid=ids.subCategoryId;
console.log(“subcatId”,subcatId);
log(“subcatId.length”,subcatId.length)
设serId=ids.serviceId;
控制台日志(“serId”,serId);
console.log(“subcatId.length”,serId.length)
让作业=新作业(ID);
设catgy=[];
设subcatgy=[];
设subser=[];
对于(i=0;i{
if(类别和类别!=null){
console.log('category.name',category.name);
catgy.push(`${category.name}`)
控制台日志(catgy);
}
})
}
对于(i=0;i{
if(服务和服务!=null){
log('service.name',service.name);
subcatgy.push(`${service.name}`)
console.log(subcatgy);
}
})
}
对于(i=0;i{
if(子服务和子服务!=null){
console.log('subservice.name',subservice.subservice);
subser.push(`${subService.subService}`)
控制台日志(海底);
}
})
}
var category=catgy.slice();
console.log(“category1”,category)
var service=subcatgy.slice();
console.log('service2',service);
var subService=subser.slice();
控制台日志(“子服务3”,子服务);
控制台日志(作业);
job.save()。然后(job=>{
res.status(201.json)({
消息:“作业已成功保存”,作业
})
}).catch(err=>console.log(err));
});

问题在于已解析承诺的回调函数(
findById
)是异步执行的。尝试签出,我想这会对您有所帮助。

我有一个不同的解决方案来推送数据。。但是谢谢你给我的链接给了我一个粗略的想法@雷纳托瓦索
const jobSch = new Schema({
    token : {type:Number},
    name : {type:String},
    phoneNumber: {type:Number},
    gender : {type:String},
    stage : {type:String},
    categoryId : [Schema.Types.ObjectId],
    subCategoryId : [Schema.Types.ObjectId],
    serviceId : [Schema.Types.ObjectId],
    category : [String],
    subcategory : [String],
    service: [String],
    paymenttype: [String],
    tax: {type:Number},
    totalprice: {type:String},
    servicePerson: {type:String},
    createdOn : {type:Date, default:new Date()},
    updatedOn : {type:Date, default:new Date()}   
});
router.post('/', async (req, res) => {
    let ids = req.body;
    let catId = ids.categoryId;
    console.log("catId", catId)
    console.log("catId.length", catId.length)
    let subcatId = ids.subCategoryId;
    console.log("subcatId", subcatId);
    console.log("subcatId.length", subcatId.length)
    let serId = ids.serviceId;
    console.log("serId", serId);
    console.log("subcatId.length", serId.length)
    let job = new Job(ids);
    let catgy=[];
    let subcatgy=[];
    let subser=[];

    for (i = 0; i < catId.length; i++) {
        Category.findById(catId[i])
            .then(category => {
                if (category && category != null) {
                    console.log('category.name', category.name);
                    catgy.push(`${category.name}`)
                    console.log(catgy);

                }
            })
    }
    for (i = 0; i < subcatId.length; i++) {
        Service.findById(subcatId[i])
            .then(service => {
                if (service && service != null) {
                    console.log('service.name', service.name);
                    subcatgy.push(`${service.name}`)
                    console.log(subcatgy);

                }
            })
    }
    for (i = 0; i < serId.length; i++) {
        Subservice.findById(serId[i])
            .then(subService => {
                if (subService && subService != null) {
                    console.log('subservice.name', subService.subService);
                    subser.push(`${subService.subService}`)
                  console.log(subser);
                }
            })
    }
    var category = catgy.slice();
    console.log("category1", category)
    var service = subcatgy.slice();
    console.log('service2', service);
    var subService = subser.slice();
    console.log('subService3',subService);
    console.log(job);
    job.save().then(job => {
        res.status(201).json({
            message: 'Job saved successfully', job
        })
    }).catch(err => console.log(err));
});