我需要如何根据这些代码添加 Json 文件?

Posted

技术标签:

【中文标题】我需要如何根据这些代码添加 Json 文件?【英文标题】:How do I need to add a Json file according to these code? 【发布时间】:2017-05-06 06:57:21 【问题描述】:

大家好,我不知道如何根据这些代码添加json文件。 我想创建一个包含姓名、姓氏、年龄、电话、mevki_id、城市和城镇的用户。

注意:城市和城镇在土耳其语 ililce 中的意思。

这是我的代码:

用户架构:

var userSchema = Mongoose.Schema( 

    name:type: String,require:true,
    surname: type: String,require:true,
    tel: type: String,require:true,
    age: type: String,require:true,
    mevki_id: type: String,require:true,
    location_id :  type : Mongoose.Schema.Types.ObjectId , ref: 'locations'

位置架构:

var LocationSchema = Mongoose.Schema (

    il: type: String, require:true,

    ilce: type:String, require:true   

);

用户控制器:

this.createUser = function (req, res, next) 


      user.name = req.body.name;
        user.surname = req.body.surname;
        user.tel = req.body.tel;
        user.age = req.body.age;
        user.mevki_id= req.body.mevki_id;           
      user.location_id=location._id.id;

 user.save(function(err, result) 


        if (err) 

            return res.send('error':err); 
        
        else 
    return res.send('result':result,'status':'successfully saved');
  
    );
;

我需要用 postman 插入什么样的 json 文件??

【问题讨论】:

我不确定你在问什么。您希望 JSON 文件将用户添加到您的数据库吗? 是的,我想要这个,但实际上我不知道我想知道如何向用户添加文档城市和城镇信息,因为用户模式中没有城市和数据,它们在位置模式中所以什么样将 json 文件添加到我的数据库中?谢谢 :) 【参考方案1】:

很明显,您的 POST 查询数据应该以这种格式发送:


    name: 'name',
    surname: 'surname',
    tel: 'tel',
    age: 'age',
    mevki_id: 'mevki_id',
    location_id: 'location_id'

我无法确定控制器中的 location._id.id 变量,因为您没有显示它的来源(在代码的早期?),但您需要传递位置 id作为对数据库中某个位置的引用,对应于 userSchema location_id 类型。 此 id 是对从 LocationSchema 定义的位置的 ObjectId 引用。 您可以省略此元素,因为它在架构中不是必需的

请记住,架构只是数据的结构,而不是数据本身。

【讨论】:

我真的很感谢你 TGrif,你真的是个乐于助人的人 如果要感谢他,记得验证答案;)

以上是关于我需要如何根据这些代码添加 Json 文件?的主要内容,如果未能解决你的问题,请参考以下文章

需要 ext-fileinfo。如何将其添加到我的 composer.json 文件中?

如何在 Objective C 中形成 JSON 格式的可变数组

从 Json 中选择值以添加到下拉列表

如何从文件中流式传输 JSON?

基于json文件动态显示选择、复选框、日期选择器等

根据 json 数据生成字段 - 如何解决选择/复选框