AssertionError [ERR_ASSERTION]:无法包装错误

时间:2019-03-08 09:28:45

标签: javascript node.js mongodb

最近,我开始使用Hapi nodejs框架。我正在使用

“ hapi@18.1.0”我只是使用mongoDB编写Create API。

。如果使用邮递员将其击中api,则显示错误,这是

我在server.js中的代码

AssertionError [ERR_ASSERTION]:无法包装错误

server.js

const Hapi = require('hapi');

const mongoose = require('mongoose');

const routes = require('./routes/route.js');

mongoose.connect("mongodb://localhost:27017/test", { useNewUrlParser: true }, err => {

    if(!err){

        console.log('mongodb connection success')
    } else {

        console.log('error:' + err)
    }
})
const server = new Hapi.server({ port: 3000, host: 'localhost' });

server.route({
    path: '/',
    method: 'GET',
    handler(req, reply) {
        reply('Welcome to HapiJs course!!');
    }
});
server.route(routes);

const start = async function() {

    try {
        await server.start();
    } catch (err) {
        console.log(err);
        process.exit(1);
    }

    console.log('Server running at:', server.info.uri);
};

start();

controller.js文件

const user = require('../models/model.js');

module.exports = {

    create(req, h) {
        if (!req.payload.username) {

            return h.response({ er: 'name is required field' }).code(400);
        }
        user.create({

            username: req.payload.name,
            password: req.payload.password,
            role: req.payload.role
        }, (err, user) => {

            if (err) {
                return h.response(err).code(500);
            }
            return h.response(user);
        });
    },
    find(req, h) {

        user.find({}, (err, user) => {
            if (err) {
                return h.response(err).code(404);
            }
            return h.response(user);
        })
    }
};

0 个答案:

没有答案