json-schema-faker生成新数据

时间:2018-01-29 12:44:19

标签: json-server json-schema-faker

我是使用json-server和json-schema-faker的新手。我可以让它生成记录但是当我发布到端点时,它发布到数据库的唯一的东西是一个带有id的对象,为什么它没有其他字段(即name,pro1,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"));
    }
});

1 个答案:

答案 0 :(得分:1)

在POST请求的Header-s部分,必须有标题:“ Content-Type:application / json

在POST请求的正文部分('原始'对象)中,写入对象,必须像这样对服务器进行后期处理:

{
    "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"
}