fastify在模型中是不确定的

问题描述 投票:0回答:1

我正在努力满足于fastify-bookshelfjs。

联系方式(型号)

module.exports = async function (fastify) {
  console.log("4")
  fastify.bookshelf.Model.extend({
    tableName: 'contacts',
  })
}

联系(控制器)

console.log("3")
const Contact = require('../models/contact')()

// Get all contact
async function getContact(req, reply) {
        const contacts = Contact.fetchAll()
        reply.code(200).send(contacts)
}
module.exports = getContact

联系(路线)

module.exports = async function (fastify) {
  console.log("2")
  const contact = require('../controller/contact')

  fastify.get('/', contact.getContact)
}

当服务器启动时,我得到这个输出

2
3
4
(node:10939) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'bookshelf' of undefined
1
server listening on 3000

为什么在联系(模型)中取证是不确定的,如何解决?

node.js bookshelf.js fastify
1个回答
0
投票

在你的controller中,当你导入模型时,你需要把fastify作为参数。

此外,您必须导入fastify模块。

你的联系人(控制人)应该是

const fastify = require('fastify') // import the fastify module here
console.log("3")
const Contact = require('../models/contact')(fastify)

// Get all contact
async function getContact(req, reply) {
        const contacts = Contact.fetchAll()
        reply.code(200).send(contacts)
}
module.exports = getContact
© www.soinside.com 2019 - 2024. All rights reserved.