繁体   English   中英

TypeError:无法读取未定义 MongoDB NodeJS 的属性

[英]TypeError: Cannot read property of undefined MongoDB NodeJS

我正在尝试制作在线发票管理系统,但在调用 function 类型错误时出现以下错误TypeError: Cannot read property 'logo' of undefined以下是我的文件:

发票 Model JS:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const InvoiceSchema = new Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    company: {
        type : Object,
        ref: 'company'
    },
    clientDetails: {
        Name: { type: String },
        phone: { type: String },
        Address: { type: String },
    },
    productDetails: {
        Name: { type: String },
        grossPrice: { type: String },
        qty: { type: Number },
        tax: { type: String },
        netPrice: { type: String }
    },
    Date: { type: Date, default: Date.now }

}, { timestamps: true });

const Invoice = mongoose.model('invoice', InvoiceSchema);
module.exports = Invoice;

Company.js Model(参考 Invoice.js 模型)

const mongoose = require('mongoose');
const { Schema } = mongoose;

const CompanySchema = new Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    Name: { type: String, default: "TEST" },
    Address: { type: String },
    pincode: { type: String },
    city: { type: String },
    phone: { type: Number },
    GST: { type: String },
    logo: { type: String },
    date: { type: Date, default: Date.now }
}, { timestamps: true });

const Company = mongoose.model('company', CompanySchema);
module.exports = Company;

invoice.js 路由器:

router.post('/printInvoice/:id', fetchuser, async (req, res) => {
    try {

        const invoice = await Invoice.find({invoice : req.params.id});

        // if (!invoice) { return res.status(404).send({ Error: "Invoice not found" }) }

        const pdfInvoice = await printInvoice(invoice);
        console.log(pdfInvoice)

        res.json("Invoice genrated")
    } catch (error) {
        console.log(error);
        res.status(500).send("Internal Server Error")
    }

})

module.exports = router;

***打印发票()function:***

 const easyinvoice = require("easyinvoice")
const fs = require('fs');

async function printInvoice(invoice){

    let data = {
        //"documentTitle": "RECEIPT", //Defaults to INVOICE
        //"locale": "de-DE", //Defaults to en-US, used for number formatting (see docs)
        "currency": "INR", //See documentation 'Locales and Currency' for more info
        "taxNotation": "gst", //or gst
        "marginTop": 25,
        "marginRight": 25,
        "marginLeft": 25,
        "marginBottom": 25,
        "logo": (invoice.company.logo ? invoice.company.logo : null), //or base64
        "background": "https://public.easyinvoice.cloud/img/watermark-draft.jpg", //or base64 //img or pdf
        "sender": {

            "company": invoice.company.Name || null,
            "address": invoice.company.Address || null,
            "zip": invoice.company.pincode || null,
            "city": invoice.company.city || null,
            "GST": invoice.company.gst || null,
            "country": "India",

        },
        "client": {
            "company": invoice.clientDetails.Name || null,
            "address": invoice.clientDetails.Address || null,
            "zip": invoice.clientDetails.pincode || null,
            "city": invoice.clientDetails.city || null,
            "country": invoice.clientDetails.country || null,
            "phone": invoice.clientDetails.phone || null,
            //"custom2": "custom value 2",
            //"custom3": "custom value 3"
        },

        "invoiceNumber": invoiceNumber,
        "invoiceDate": Date.now(),
        "products": [invoice.productDetails],
    }

    const pdfInvoice = await easyinvoice.createInvoice(data, (result) => { console.log("Invoice created", result.pdf) })
    // await fs.writeFileSync("invoice.pdf", pdfInvoice.pdf, 'base64');
    // await easyinvoice.download('Invoice.pdf', pdfInvoice.pdf);
    // await easyinvoice.render('pdf', pdfInvoice)

    return pdfInvoice
}

module.exports = printInvoice;

安装了所有必要的导入和包。 我正在使用“easyinvoice”npm package。

谢谢你 _/_

我想您正在尝试按此处的InvoiceSchema中不存在的字段invoice查找发票:

const invoice = await Invoice.find({invoice : req.params.id});

尝试使用字段_id或方法 findById 修复查询。 像这样的东西:

const invoice = await Invoice.find({ _id: req.params.id });
// or
const invoice = await Invoice.findById(req.params.id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM