簡體   English   中英

mongoose 找到返回空數組,即使它們是數據庫上的值

[英]mongoose find returning empty array even their is a value on the database

我正在嘗試將商品數據添加到我的 stockrecord 集合中,如果商品名稱已經在 stockrecord 集合中,我只想將商品數量的數量添加到我的 stockrecord 數量中。

但即使它們是現有數據,find 方法也返回一個空數組

這是我的代碼

  commodity.map(async (e) => {
    const data = await new Commodity({
      name: e.commodityName,
      units: e.units,
      quantity: e.quantity,
    });
    data.donator = donator;
    await data.save();
    const stock = await StockRecord.find({
      name: {
        $eq: e.commodityName,
      },
    });
    //console.log(stock);

    if (stock.length === 0) {
      const record = await new StockRecord({
        name: e.commodityName,
        units: e.units,
        quantity: parseFloat(e.quantity),
      });
      await record.save();
      console.log(record);
    } else {
      console.log('may sulud');

      stock[0].quantity += parseFloat(e.quantity);
      await stock[0].save();
    }
  });

這是我的庫存記錄 model

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const stockRecordSchema = new Schema({
      name: {
        type: String,
      },
    
      units: {
        type: String,
        enum: \['kg', 'pcs'\],
      },
      quantity: {
        type: Number,
      },
    });

Array.map 不接受承諾

嘗試使用for (const item of commodity) { //async magic here }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM