安装了5.9.18版本的mongoose,插入数据库的商品中没有数据?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
电磁护盾 提问者 2022-07-02 18:54:28

解决问题了,原来是Order.js中写的有点问题。

电磁护盾 提问者 2022-07-02 18:22:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * @description 订单数据操作
 */
 
// 引用订单模型
const Order = require('../../models/Order')
// 引入地址model
const Adress = require('../../models/Address')
const Product = require('../../models/Product')
 
 
// 定义自执行匿名函数
!(async () => {
 
    //创建订单
    const requestBody = {
        addressId: '62be4c7b1a01bc4ecf8bd998',
        shopId: '62bee2b74aa42c8476c08652',
        shopName: '沃尔玛',
        isCanceled: false//订单是否被取消
        // 去数据库找沃尔玛的商品
        products: [
            {
                id: '62bef44147ee77102dce56e1'// 复制?引用?
                num: 3 //购买数量
            },
            {
                id: '62bef44247ee77102dce56e4',
                num: 5 //购买数量
            }
        ]
    }
    // 通过addressId获取address
    const adress = Adress.findById(requestBody.addressId)
 
    // 根据商品id获取商品信息
    // 获取商品列表
 
    // 用数组的map函数可以取出商品id,map函数返回p.id
    // 会得到一个数组pIds,里面分别是商品1的id,商品2的id
    const pIds =requestBody.products.map(p => p.id) // ['商品1 id'],['商品2 id']
 
    // 根据商品id去数据库中查找商品列表
    const productList = await Product.find({
        // 查询条件
        shopId: requestBody.shopId, //沃尔玛的商品
        // 判断当前查询的商品的_id要在数组pIds中
        _id: {
            $in: pIds
        }
    })
 
    // console.log(pIds) // [ '62bef44147ee77102dce56e1', '62bef44247ee77102dce56e4' ]
    // console.log(productList)
 
 
    //整合订单购买数量
    const productListWithSales = productList.map(p => {
        // 商品 id,获取字符串形式
        const id = p._id.toString()
 
        // 找到商品的购买数量。   知道商品id,就能找到商品的购买数量
        // 把requestBody.products用filter()进行筛选。从数组中找出id(item.id)能等于当前商品id的
        const filterProducts = requestBody.products.filter(item => item.id === id)
        // 如果过滤失败了了
        if (filterProducts.length === 0) {
            throw new Error('未找到匹配的销量数据')
        }
 
        // console.log(p+'<-----------1');
        // console.log(filterProducts[0]+'<-----------2');
 
        return {
            product: p,
            // mongoose6版本的写法
            // product: {
            //     shopId: p.shopId,
            //     name: p.name,
            //     imgUrl: p.imgUrl,
            //     sales: p.sales,
            //     price: p.price,
            //     oldPrice: p.oldPrice
            // },
            orderSales: filterProducts[0].num
        }
    })
    console.log(productListWithSales);
 
 
    // 创建订单
    await Order.create({
        // 这里是确定的用户,所以直接写
        username: 'zhangsan',
        shopId: requestBody.shopId,
        shopName: requestBody.shopName,
        isCanceled: requestBody.isCanceled,
        products: productListWidthSales
    })
 
 
})()


  • 提问者 电磁护盾 #1

    第97行把productListWidthSales改成productListWithSales

    2022-07-02 18:24:31
电磁护盾 提问者 2022-07-02 18:20:44

https://img1.sycdn.imooc.com//climg/62c01b8d09a7ed3907240496.jpg

productListWithSales可以打印出来



  • 同学你好,代码需要做如下调整:

    1、地址需要使用await获取到具体的数据:

    https://img1.sycdn.imooc.com//climg/62c02256091e7bd209470112.jpg

    2、创建订单时,要使用adress:

    https://img1.sycdn.imooc.com//climg/62c0229509b0be7007650339.jpg

    祝学习愉快!

    2022-07-02 18:49:07
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码