我正在尝试使用 Fastify 和 fastify-monogdb。目前我有以下...
在我的/src/index.js
const routes = require("./routes");
const fastify = require("fastify")({
logger: true
});
routes.forEach((route, index) => {
fastify.route(route);
});
fastify.register(require("fastify-mongodb"), {
url: "mongodb://localhost:27017/parkedcars"
});
const startFastify = async () => {
try {
await fastify.listen(3333);
fastify.log.info(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
startFastify();
在我的/routes/index.js我有一条路线...
const carController = require("../controllers/carController");
{
method: "POST",
url: "/api/create/parkedcar",
handler: carController.createParkedCar
}
最后在我的/controllers/carController ...
const fastify = require("fastify")();
exports.createParkedCar = async (req, reply) => {
try {
let car = { ...req.body };
const db = fastify.mongo.db
*// will do insert here*
return car;
} catch (err) {
throw boom.boomify(err);
}
};
当我尝试调用时:
const db = fastify.mongo.db
我收到一条错误消息...
“无法读取未定义的属性 'db'”
我在这里做错了什么?在这一点上mongo
是如何未定义的?
“fastify.register”不是让我可以访问吗?