Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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
Javascript Mongodb:如何通过api填充模式中引用的模式_Javascript_Mongodb_Typescript_Express_Schema - Fatal编程技术网

Javascript Mongodb:如何通过api填充模式中引用的模式

Javascript Mongodb:如何通过api填充模式中引用的模式,javascript,mongodb,typescript,express,schema,Javascript,Mongodb,Typescript,Express,Schema,我想编辑我的Api,以便能够填充引用的模式。 这是我的模式: export const taskSchema=新模式({ 用户:{ 类型:字符串, 必填项:true }, 项目:{ 类型:字符串, 必填项:true }, 问题:{ 类型:字符串, 必填项:true }, 标题:{ 类型:字符串, 必填项:true }, 记录:[{ _域:{ 类型:Schema.Types.ObjectId, 参考:“任务域” }, 时间:{ 类型:编号 } }], 创建日期:{ 类型:日期, 默认值:Date

我想编辑我的Api,以便能够填充引用的模式。 这是我的模式:

export const taskSchema=新模式({
用户:{
类型:字符串,
必填项:true
},
项目:{
类型:字符串,
必填项:true
},
问题:{
类型:字符串,
必填项:true
},
标题:{
类型:字符串,
必填项:true
},
记录:[{
_域:{
类型:Schema.Types.ObjectId,
参考:“任务域”
},
时间:{
类型:编号
}
}],
创建日期:{
类型:日期,
默认值:Date.now
}

});您当前的方法有点错误,您需要首先保存域文档,成功保存后可以创建任务文档

试试这个:

public addNewTask (req: Request, res:Response){

    // create the domain document first, before creating the task document, and then store its _id in the task document
    let _domain = new domain(req.body._domain);
    _domain.save((err,_domain)=>{
        let newTask = new Task();
        newTask.user = req.body.user;
        newTask.project = req.body.project;
        newTask.issue = req.body.issue;
        newTask.title = req.body.title;
        newTask.dateCreated = req.body.dateCreated;

        // here you only need to store the _id of the newly created _domain document
        newTask.records = [{
            _domain : _domain._id,
            time : req.body.time
        }]

        newTask.save((err, task)=>{
            if(err){
                res.send(err);
            }
            //if you want populated _domain object in your records array, you can use .populate()
            Task.populate(task,{path : records._domain},(err,task) =>                                 
            {
               res.json(task);
            })
        });
    })
}
我假设,您的请求主体如下所示:

{
    user : "user_name",
    project : "project_name",
    issue : "issue_name",
    title : "title_",
    dateCreated : "date",
    _domain : {
        label : "some_label"
    },
    time : 12345
}

非常感谢你,我现在就试试,让你知道它是否有效。如何将记录发布到postman上?我想尝试一下,看看这种方法对postman是否有效:如何填写正文:{“用户”:“salma”,“项目”:“9”,“问题”:“24”,“标题”:“task3”“记录”:[{“标签”:“测试”},{“时间”:40}}}只需像这样尝试:
{“用户”:“salma”,“项目”:“9”,“问题”:“24”,“title”:“task3”,“_域”:{“label”:“test”},“time”:40}
。如果您按照之前评论中的建议发送请求,您将不得不更改我给出的代码,因为我得到以下信息:{“_id”:“5c9c5456d5ef022b6db2f88e”,“记录”:[{“_id”:“5c9c5456d5ef022b6db2f88f”,“时间”:40}],“用户”:“salma”,“项目”:“9”,“问题”:“24”,“标题”:“task3”,“__v”:0}标签未发布标签也已发布,但由于它是另一个文档(引用文档),它保存在域文档中,请参阅您在架构中使用了
type:Schema.Types.ObjectId
。如果您想在保存后也填充域,则需要使用
.populate()
。请阅读参考文档以及如何在mongoose中使用它们