oracledb 类型错误:架构不是构造函数

问题描述

我在控制台上部署模型时遇到错误,我收到此提示

TypeError: Schema is not a constructor

我对 Javascript 非常陌生,所以我不明白出了什么问题。根据日志中的提示,我在模型中的 2 个档案以及我的路线中的另一个档案中收到该错误

这是我模型上的代码

const oracledb = require('oracledb')
const Schema = oracledb.Schema

const Meals = oracledb.model('Meal',new Schema({ //I get the error at this line on new Schema
name: String,desc: String,}))

module.exports = Meals

我的路线上的代码

const express = require ('express')
const Meals = require ('../models/Meals') //I get the error also here

const router = express.Router()

router.get('/',(req,res) =>{
Meals.find()
.exec()
.then(x => res.status(200).send(x))
})

router.get('/:id',res) => {
Meals.findById(req.params.id)
.exec()
.then(x => res.status(200).send(x))
})

router.post('/',res) => {
Meals.create(req.body).then(x => res.status(201).send(x))
})

router.put('/:id',res) => {
Meals.findOneAndUpdate(req.params.id,req.body)
.then(() => res.sendStatus(204))
})

router.delete('/:id',res) => {
Meals.findOneAndDelete(req.params.id).exec().then(() => res.sendStatus(204))
})
module.exports = router

那么..我在这里做错了什么?

解决方法

错误告诉您,您尝试实例化 Schema 类型的对象但未成功,因为您尝试调用不存在的构造函数。让我把它分解给你,一个概念一个概念

课程

类是其特定类型对象的一般定义。类是一个通用集合,可以实例化为对象。这是一个基本的 OOP 概念,因此我建议您也阅读有关 OOP(面向对象编程)的内容。

所以,如果你的对象是一个人,那么你的班级代表每个人。这就是类和对象的关系。

我还应该提到,您也可以将函数实例化为对象,但是,为了避免混淆,我现在不会深入研究这一点。

实例

实例是基于类实例化的对象。它可以通过构造函数调用来实例化,比如

let myInstance = new MyClass();

其中 MyClass 是您实例化的类,myInstance 是生成的对象/实例。

你的问题

const Meals = oracledb.model('Meal',new Schema({ //I get the error at this line on new Schema
name: String,desc: String,}))

此代码尝试实例化 Schema 并将结果对象作为 oracledb.model 调用的第二个参数传递。您需要查看Schema。您已经通过

创建了这样一个对象
const Schema = oracledb.Schema

因此,您需要了解如何实例化 Schema 并相应地更改您的代码。