【问题标题】:json-schema-faker generate new datajson-schema-faker 生成新数据
【发布时间】:2018-07-08 04:02:07
【问题描述】:

我不熟悉使用 json-server 和 json-schema-faker。我可以让它生成记录,但是当我发布到端点时,它发布到数据库的唯一内容是具有 id 的对象,为什么它没有其他字段(即名称、p​​ro1、pro2 等)。有人可以帮忙吗?

以下是我的架构:

    export const schema = {
    "type": "object",
    "properties": {
        "tests": {
            "type": "array",
            "minItems": 1,
            "maxItems": 6,
            "items": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "number",
                        "unique": true,
                        "minimum": 1
                    },
                    "name": {
                        "type": "string"
                    },
                    "pro1": {
                        "type": "integer",
                        "minimum": 0,
                        "maximum": 100
                    },
                    "pro2": {
                        "format": "date-time",
                        "type": "string"
                    },
                    "pro3": {
                        "type": "boolean"
                    },
                    "pro4": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10
                    },
                    "pro5": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10
                    }
                },
                "required": ["id", "name", "pro1", "pro2", "pro3", "pro4", "pro5"]
            }
        }
    },
    "required": ["tests"]
};

还有我的生成文件:

import jsf from 'json-schema-faker';
import {schema} from './mockDataSchema';
import fs from 'fs';
import chalk from 'chalk';

const json = JSON.stringify(jsf(schema));

fs.writeFile("./src/api/db.json", json, function(err){
    if (err){
        return console.log(chalk.red(err));
    }else{
        return console.log(chalk.green("Mock Data Generated"));
    }
});

【问题讨论】:

    标签: json-server json-schema-faker


    【解决方案1】:

    在 POST-request 的 Header-s 部分,必须有 header : "Content-Type: application/json"

    在 POST 请求的正文部分('raw' 对象)中,写入对象,必须像这样发布到服务器:

    {
        "id": 1,
        "name": "John Doe",
        "email": "John.Doe@mailserver.com"
    }
    

    例如:使用 POSTMAN(http 客户端应用程序), POST 请求是这样的:

    POST /users HTTP/1.1
    Host: 127.0.0.1:3001
    Content-Type: application/json
    Cache-Control: no-cache
    Postman-Token: some postman token
    
    {
        "id": 1,
        "name": "John Doe",
        "email": "John.Doe@mailserver.com"
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 2022-01-17
      • 1970-01-01
      • 2019-04-29
      • 2023-03-30
      • 2015-03-07
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多