javascript - 猫鼬找到返回空数组,即使它们是数据库上的值
问题描述
我正在尝试将商品数据添加到我的 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();
}
});
这是我的 stockrecord模型
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 }
推荐阅读
- asp.net - 使用 .net core 和 razor,如何从静态页面导航回 asp 菜单
- python - 如何将 Django 表单字段名称列表传递给模板?
- reactjs - 如何设置 react createtableselect 以防止创建不在选项中的元素?
- php - 如何使用循环在 DataTables 中添加按钮
- anaconda3 - 无法初始化 anaconda 目录
- python - 如何在 python 中将 CatPart 文件转换为 STL 格式?
- flutter - 带有滚动、浮动按钮和固定行的扩展列
- javascript - VueJs如何记住表单中的输入数据,即使在上一页/下一页之后并返回表单页面?
- python - SQLALCHEMY_ECHO 的关键错误 = Flask_Sqlalchemy 的 False
- google-apps-script - 谷歌脚本在测试部署上工作,但在版本部署后不工作