Mongodb :值未保存在 db 中,但存在于架构的 console.log 中
Mongodb : value not beign saved in db , but present in console.log of schema
我是 mongo 数据库的新手,我正在尝试将图像的 url 保存到 mongo 数据库中,但该值并未保存。但是当我做 console.log 时,url 出现在模式中
在 .save() 之前;
cloudinary.uploader.upload(file.photo.path, { folder: 'products' }, function (err, image) {
if (err) {
return res.status(400).json({
error : "Unable to upload the image"
})
}
console.log(product.photo);
console.log(image.url);
product.photo = image.url;
console.log(product.photo);
console.log(product);
});
console.log(product);
的输出:
{ sold: 0,
_id: 5edc1830887d5801c88420d9,
name: 'ider',
description: 'classic prod',
price: 10,
category: 5ed9258ae973f42bbcbef42b,
stock: 27,
createdAt: 2020-06-06T22:26:56.136Z,
updatedAt: 2020-06-06T22:26:56.136Z,
__v: 0,
photo: 'http://res.cloudinary.com/xxx/image/upload/v1591482420/products/iwsutsgezodvvbyvopnf.png' }
产品节省:
//save to db
product.save((err, prod) => {
if(err) {
return res.status(400).json({
error : "Saving of product failed"
})
}
res.json(prod);
})
回复:
{
"sold": 0,
"_id": "5edc1830887d5801c88420d9",
"name": "ider",
"description": "classic prod",
"price": 10,
"category": "5ed9258ae973f42bbcbef42b",
"stock": 27,
"createdAt": "2020-06-06T22:26:56.136Z",
"updatedAt": "2020-06-06T22:26:56.136Z",
"__v": 0
}
照片的定义方式是模式:photo: String
完整架构:
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const productSchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
maxlength: 32
},
description: {
type: String,
trim: true,
required: true,
maxlength: 2000
},
price: {
type: Number,
required: true,
maxlength: 32,
trim: true
},
category: {
type: ObjectId,
ref: "Category",
required: true
},
stock: {
type: Number
},
sold: {
type: Number,
default: 0
},
photo: String
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", productSchema);
我也用 robo 3t 检查过,数据库中没有照片值
在异步上传完成之前正在保存产品(如评论中所确认)。
在图片上传回调中保存产品应该可行:
cloudinary.uploader.upload(file.photo.path, { folder: 'products' }, function (err, image) {
if (err) {
return res.status(400).json({
error : "Unable to upload the image"
})
}
//save to db
product.save((err, prod) => {
if(err) {
return res.status(400).json({
error : "Saving of product failed"
})
}
res.json(prod);
})
});
我是 mongo 数据库的新手,我正在尝试将图像的 url 保存到 mongo 数据库中,但该值并未保存。但是当我做 console.log 时,url 出现在模式中 在 .save() 之前;
cloudinary.uploader.upload(file.photo.path, { folder: 'products' }, function (err, image) {
if (err) {
return res.status(400).json({
error : "Unable to upload the image"
})
}
console.log(product.photo);
console.log(image.url);
product.photo = image.url;
console.log(product.photo);
console.log(product);
});
console.log(product);
的输出:
{ sold: 0,
_id: 5edc1830887d5801c88420d9,
name: 'ider',
description: 'classic prod',
price: 10,
category: 5ed9258ae973f42bbcbef42b,
stock: 27,
createdAt: 2020-06-06T22:26:56.136Z,
updatedAt: 2020-06-06T22:26:56.136Z,
__v: 0,
photo: 'http://res.cloudinary.com/xxx/image/upload/v1591482420/products/iwsutsgezodvvbyvopnf.png' }
产品节省:
//save to db
product.save((err, prod) => {
if(err) {
return res.status(400).json({
error : "Saving of product failed"
})
}
res.json(prod);
})
回复:
{
"sold": 0,
"_id": "5edc1830887d5801c88420d9",
"name": "ider",
"description": "classic prod",
"price": 10,
"category": "5ed9258ae973f42bbcbef42b",
"stock": 27,
"createdAt": "2020-06-06T22:26:56.136Z",
"updatedAt": "2020-06-06T22:26:56.136Z",
"__v": 0
}
照片的定义方式是模式:photo: String
完整架构:
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const productSchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: true,
maxlength: 32
},
description: {
type: String,
trim: true,
required: true,
maxlength: 2000
},
price: {
type: Number,
required: true,
maxlength: 32,
trim: true
},
category: {
type: ObjectId,
ref: "Category",
required: true
},
stock: {
type: Number
},
sold: {
type: Number,
default: 0
},
photo: String
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", productSchema);
我也用 robo 3t 检查过,数据库中没有照片值
在异步上传完成之前正在保存产品(如评论中所确认)。
在图片上传回调中保存产品应该可行:
cloudinary.uploader.upload(file.photo.path, { folder: 'products' }, function (err, image) {
if (err) {
return res.status(400).json({
error : "Unable to upload the image"
})
}
//save to db
product.save((err, prod) => {
if(err) {
return res.status(400).json({
error : "Saving of product failed"
})
}
res.json(prod);
})
});