Javascript 在Webstorm 10.0中使用hapijs和Rest客户端创建POST方法并对其进行测试

Javascript 在Webstorm 10.0中使用hapijs和Rest客户端创建POST方法并对其进行测试,javascript,mongodb,rest,webstorm,hapijs,Javascript,Mongodb,Rest,Webstorm,Hapijs,我正在尝试为POST方法构建一个路由,并测试它是否能够与Webstorm 10.0中提供的Rest客户端一起工作。我已经阅读了API和许多关于hapijs和rest客户端的stackOverflow问题。我已经建立了GET方法的路线,并成功地进行了测试。不过,我正在努力处理post,尤其是读取有效负载并通过rest客户端提交请求。基本上,我试图在mongodb数据库中发送一个形式为{brand:'brandName}的car对象,例如{“\u id”:ObjectId(“aisjdioajsoi

我正在尝试为POST方法构建一个路由,并测试它是否能够与Webstorm 10.0中提供的Rest客户端一起工作。我已经阅读了API和许多关于hapijs和rest客户端的stackOverflow问题。我已经建立了GET方法的路线,并成功地进行了测试。不过,我正在努力处理post,尤其是读取有效负载并通过rest客户端提交请求。基本上,我试图在mongodb数据库中发送一个形式为
{brand:'brandName}
的car对象,例如
{“\u id”:ObjectId(“aisjdioajsoidj”),“brand”:“Kia”}
显示在mongodb shell中

这就是我到目前为止对路线的了解:

server.route({
method: 'POST', 
path: '/{type}/{brand}', //only designed to post cars types currently
handler: function(request,reply){ 
    var postPayload = request.payload; //even after I submit text through the rest client postPayload never takes on request.payloads value. Also, request.payload is a string that can't be converted to an object with JSON.parse()
    var postPromise;
    dao1.create2('car',request.payload); //I send the payload to my data access object to handle it, where the post request can be validated with JSON schema for 'car'
    //postPromise = users.insert({brand:request.payload}) this works but, i can't validate request.payload with JSON schema
}
});
这是我的POST数据访问对象的代码:

var DAO = function(){ //dao1 is an instance of DAO
    this.create2 = function(schema, input){ //this method works fine when I hardcode in the input with something like {brand:"Kia"} so it's clearly getting the wrong type of data
        this.errors = env.validate(schema, input);
        this.promise;
        if (!this.errors) {
            if(schema=='car'){
                this.promise = users.insert(input,function(error, results){ //this is a method from monk (an npm package for mongodb)
                    if(error){
                        console.log("Error 400 Bad Request "+error);
                }
                    else{
                        console.log("User's car validated"+JSON.stringify(results));
                    }
                })
            }
        }
    }
}

也许我的问题在于我如何与Rest客户机交互。我在请求头中添加了
内容类型:application/json
。为了提交post请求,我在请求正文部分的文本字段中输入了
“{”brand:“Toyota2.3”}”
。我还尝试了
car={“brand”:“Toyota2.3”}
这两个选项都出现在调试器中,比如
“{”brand:“Toyota2.3”}
。但是,当我将request.payload放入验证器时,我得到了错误
{validation:{type:'object'}}
。我似乎没有办法将这些信息转换成一个对象。还要注意,我将方法设置为在调试器中POST,并在主机/端口中填入
http://localhost:8000
我在路径中填入了
/car/Kia
(当我添加GET路径时,所有这些都有效)。我的想法是,是否有必要将
request.payload
转换为对象?还是我使用rest客户端的方式不正确?也许我想我会感激你的建议,谢谢

您是否尝试过其他客户机,如POSTMAN?不,也许值得一试。您能够解决您的问题吗?我能够有效地与POSTMAN进行调试。仍然无法理解Rest的版本。谢谢你的帮助!