Javascript TypeError:架构路径“product.type”的值无效,获取的值为;“未定义”;
我正在处理一个电子商务应用程序,在运行服务器时遇到错误,请参阅附件以供参考。当我使用身份验证时,它对我来说很好,但是当我开始在NodeJS中使用populate时,我开始出现错误,我无法解决这个问题 找到下面的代码 models/product.jsJavascript TypeError:架构路径“product.type”的值无效,获取的值为;“未定义”;,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我正在处理一个电子商务应用程序,在运行服务器时遇到错误,请参阅附件以供参考。当我使用身份验证时,它对我来说很好,但是当我开始在NodeJS中使用populate时,我开始出现错误,我无法解决这个问题 找到下面的代码 models/product.js const mongoose = require("mongoose"); const { ObjectId } = mongoose.Schema; const productSchema = new mongoose.S
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: {
data: Buffer,
contentType: String
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Product", productSchema);
models/user.js
const mongoose = require('mongoose')
const crypto = require('crypto')
const uuidv1 = require('uuid/v1')
// User Schema design in DB with mongoose
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
maxlength: 32,
trim: true
},
lastname: {
type: String,
maxlength: 32,
trim: true
},
email: {
type: String,
trim: true,
required: true,
unique: true,
},
userinfo: {
type: String,
trim: true
},
encry_password: {
type: String,
required: true,
},
salt: String,
role: {
type: Number,
default: 0
},
purchases: {
type: Array,
default: []
}
}, {timestamps: true})
// Virtual field creation
// Reffering the virtual method with password and it will be stored in encry_password in DB
userSchema.virtual('password')
.set(function(password) {
this._password = password // Convention of using private variable
this.salt = uuidv1() // populating salt with uuid
this.encry_password = this.securePassword(password)
})
.get(function() {
return this._password
})
// Crypto format for password storing in DB
userSchema.methods = {
// For authentication of user
authenticate: function(plainpassword) {
return this.securePassword(plainpassword) === this.encry_password
},
// For securing password
securePassword: function(plainpassword) {
if (!plainpassword) return ""
try {
return crypto.createHmac('sha256', this.salt)
.update(plainpassword)
.digest('hex')
} catch(err) {
return ""
}
}
}
module.exports = mongoose.model('User', userSchema)
controller/user.js
const User = require('../models/user')
const Order = require('../models/order')
exports.getUserById = (req, res, next, id) => {
User.findById(id).exec((err, user) => {
if(err || !user) {
return res.status(400).json({
err: "NO USER WAS FOUND IN DB"
})
}
// Storing user in req object and profile object
req.profile = user
next()
})
}
exports.getUser = (req, res) => {
req.profile.salt = undefined
req.profile.encry_password = undefined
req.profile.createdAt = undefined
req.profile.updatedAt = undefined
return res.json(req.profile)
}
exports.updateUser = (req, res) => {
User.findByIdAndUpdate(
{_id : req.profile._id},
{$set : req.body},
{new : true, useFindAndModify: false},
(err, user) => {
if(err) {
return res.status(400).json({
err: 'YOU ARE NOT ALLOWED TO UPDATE'
})
}
user.salt = undefined
user.encry_password = undefined
res.json(user)
}
)
}
exports.userPurchaseList = (req, res) => {
Order.find({user: req.profile._id})
.populate('user', '_id name')
.exec((err, order) => {
if(err) {
return res.status(400).json({
err: "No Order in this account"
})
}
return res.json(order)
})
}
exports.pushorderInPurchaseList = (req, res, next) => {
// Created an empty array for purchase list and pushing the elements
let purchases = []
req.body.order.products.forEach(product => {
purchases.push({
_id: product._id,
name: product.name,
description: product.description,
category: product.category,
quantity: product.quantity,
amount: req.body.order.amount,
transaction_id: req.body.order.transaction_id,
})
})
User.findOneAndUpdate(
{_id: req.profile._id},
// updating purchases with local purchases array
{$push: {purchases: purchases}},
{new : true},
(err, purchases) => {
if(err) {
return res.status(400).json({
error: 'Unable to save purchase list'
})
}
next()
}
)
}
您的文件名是
user.js
和product.js
,但您正在导入require('../models/user')
和require('../models/order')
,我认为应该是require('../models/product')