简体   繁体   English

json-schema-faker生成新数据

[英]json-schema-faker generate new data

I am new to using json-server and json-schema-faker. 我是使用json-server和json-schema-faker的新手。 I can get it to generate records but when I post to the endpoint, the only thing that it posts to the database is an object with id, why doesn't it have the other fields (ie name, pro1, pro2 etc). 我可以得到它来生成记录,但是当我发布到端点时,它发布到数据库的唯一一件事就是一个带有id的对象,为什么它没有其他字段(即name,pro1,pro2等)。 Can anyone help? 有人可以帮忙吗?

Below is my schema: 以下是我的架构:

    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"]
};

And my generate file: 和我的生成文件:

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"));
    }
});

In Header-s part of POST-request, must have header : " Content-Type: application/json " 在POST请求的Header-s部分中,必须具有标头:“ Content-Type:application / json

In Body part (' raw ' object) of POST-request, write object, that have to be post-ed to server like this : 在POST请求的主体部分(“ raw ”对象)中,编写必须像这样被张贴到服务器的对象:

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

For example : with POSTMAN (http client application), the POST request is like this : 例如:对于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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM