Javascript 我用PDF打印了一些数据,节点使用Mongoose从MONGODB中提取了一些文档
我必须在PDF文件上打印一些数据,但我有一个小问题,当我调用我需要的数据时,我得到一个未定义的数据 在下一段代码中,我将展示如何获取数据库的最后一个元素并打印它 当我打开PDF时,我得到了一个JSON对象 当我打开PDF时,我得到了一个JSON对象Javascript 我用PDF打印了一些数据,节点使用Mongoose从MONGODB中提取了一些文档,javascript,node.js,mongodb,pdf,mongoose,Javascript,Node.js,Mongodb,Pdf,Mongoose,我必须在PDF文件上打印一些数据,但我有一个小问题,当我调用我需要的数据时,我得到一个未定义的数据 在下一段代码中,我将展示如何获取数据库的最后一个元素并打印它 当我打开PDF时,我得到了一个JSON对象 当我打开PDF时,我得到了一个JSON对象 SOMETHING LIKE THIS ---v { opciones:[ [ 'Producto', 'Producto 22', 'Producto 4', 'Producto 2', 'Producto 222', 'Producto w222
SOMETHING LIKE THIS ---v
{ opciones:[ [ 'Producto', 'Producto 22', 'Producto 4', 'Producto 2', 'Producto 222', 'Producto w222' ] ], _id: 5f88a46ce558897dcc34a27e, __v: 0 }
但我只需要
Producto
Producto 22
Producto 4
Producto 2
Producto 222
Producto w222
这是一个示例文本:D
在下一段代码中,我将展示如何获取数据库的最后一个元素并打印它
用于生成PDF文档的路由
const express = require('express')
const pdf = require('html-pdf')
const ProductoModel = require('../../Model/Producto/ProductoModel')
const CotizacionModel = require('../../Model/cotizacion/CotizacionModel')
const route = express.Router();
/* Generador Routes */
route.get('/get/cotizaciongen/traercotizacion', (req, res) => {
CotizacionModel.find().sort({ $natural: -1 }).limit(1).then(document => {
var contenido = `
${document}
`;
pdf.create(contenido).toFile('./Downloads/Cotizacion.pdf', function (err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
})
res.download(__dirname + '/backend/Downloads/Cotizacion.pdf')
})
route.post('/post/cotizaciongen/subiritem', (req, res) => {
CotizacionModel.find((err, document) => {
if (req.body == undefined) {
console.log(err)
res.status(500).json({
message: 'Añada algun producto'
})
} else {
const newOpcion = new CotizacionModel({
opciones: req.body.opciones
})
newOpcion.save().then(
res.status(200).json({
message: 'Opcion Agregada Satisfactoriamente'
})
)
}
})
})
module.exports = route;
猫鼬模型
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CotizacionSchema = new Schema({
opciones: {
type: Array,
required: true
}
})
module.exports = Cotizacion = mongoose.model('Cotizacion', CotizacionSchema);