0

我是使用 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"));
    }
});
4

1 回答 1

1

在 POST-request 的 Header-s 部分,必须有 header : " 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"
}
于 2018-02-04T15:23:06.023 回答