最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

表示项目未定义

IT培训 admin 15浏览 0评论

表示项目未定义

下面显示代码。

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const user = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    resetToken: String,
    resetExpiration: String,
    products: [{type: mongoose.Types.ObjectId, required: true, ref: 'Shop'}],
    cart: {
        items: [
            {
                productId: {type: mongoose.Types.ObjectId, ref: 'Shop', required: true},
                quantity: {type: Number, required: true},
            }
        ]
    },
});

user.methods.addToCart = (product) => {

    const itemIndex = this.cart.items.findIndex(prod => {
        return prod.productId.toString() === product._id.toString();
    });

    let newQuantity = 1;
    const updatedCartItems = [...this.cart.items];

    if(itemIndex >= 0) {
        newQuantity = this.cart.items[itemIndex].quantity + 1;
        updatedCartItems[itemIndex].quantity = newQuantity;
    } else {
        updatedCartItems.push({
            productId: product,
            quantity: newQuantity
        });
    }

    const updatedCart = {
        items: updatedCartItems
    }
    this.cart = updatedCart;
    return this.save();
}

const model = mongoose.model('User', user);

module.exports = model;

我尝试按照上述模式将产品存储在cart实例方法中,但是当我从控制器将产品发送到addToCart时,它说this.cart.items上的商品未定义。我在猫鼬中没有使用太多实例方法,所以我不知道这个问题是架构还是一般问题。

让我知道您是否需要其他信息。

回答如下:

这是一个愚蠢的错误,实际上我正在使用箭头功能。因此它没有绑定到架构。

表示项目未定义

下面显示代码。

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const user = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    resetToken: String,
    resetExpiration: String,
    products: [{type: mongoose.Types.ObjectId, required: true, ref: 'Shop'}],
    cart: {
        items: [
            {
                productId: {type: mongoose.Types.ObjectId, ref: 'Shop', required: true},
                quantity: {type: Number, required: true},
            }
        ]
    },
});

user.methods.addToCart = (product) => {

    const itemIndex = this.cart.items.findIndex(prod => {
        return prod.productId.toString() === product._id.toString();
    });

    let newQuantity = 1;
    const updatedCartItems = [...this.cart.items];

    if(itemIndex >= 0) {
        newQuantity = this.cart.items[itemIndex].quantity + 1;
        updatedCartItems[itemIndex].quantity = newQuantity;
    } else {
        updatedCartItems.push({
            productId: product,
            quantity: newQuantity
        });
    }

    const updatedCart = {
        items: updatedCartItems
    }
    this.cart = updatedCart;
    return this.save();
}

const model = mongoose.model('User', user);

module.exports = model;

我尝试按照上述模式将产品存储在cart实例方法中,但是当我从控制器将产品发送到addToCart时,它说this.cart.items上的商品未定义。我在猫鼬中没有使用太多实例方法,所以我不知道这个问题是架构还是一般问题。

让我知道您是否需要其他信息。

回答如下:

这是一个愚蠢的错误,实际上我正在使用箭头功能。因此它没有绑定到架构。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论