Javascript nodejs对象属性只能更改为int而不能更改为string
我刚刚收到一个wierd问题,其中下面的代码返回数组中所有对象的refno,正如预期的那样但是Javascript nodejs对象属性只能更改为int而不能更改为string,javascript,node.js,Javascript,Node.js,我刚刚收到一个wierd问题,其中下面的代码返回数组中所有对象的refno,正如预期的那样但是 let records = await Expense.find({}).exec(); // records is an array of objects [{},{}] for (let obj of records) { // the obj has a refno property which i want to change obj.
let records = await Expense.find({}).exec(); // records is an array of objects [{},{}]
for (let obj of records) { // the obj has a refno property which i want to change
obj.refno = 0 // this works as expected by changing refno property to 0
}
console.log(records)
将属性值更改为字符串的以下代码不起作用
for (let obj of records) {
obj.refno = "QM"+obj.refno
}
console.log(records) // IN this the refno. doesnt change
我的要求是将refno更改为字符串
//the object
{
_id: 5efed2c813b03d331e4dc052,
refno: 102,
project: 'EV Battery Pack',
invoiceno: 'dia',
description: 'black frame',
date: '2020-07-03',
}
因此,将属性更改为其他数字有效,但不更改为字符串,我无法了解这是如何发生的,或者我是否遗漏了什么?总之,我通过声明另一个属性并将字符串存储在其中来解决这个问题,但我不知道为什么int不能更改为对象内部的字符串
有人能解释为什么会发生这种情况吗谢谢你的帮助 编辑:费用模式
var schema = new Schema({
refno: { type: Number, require: true },
project: { type: String, require: true },
projectid: { type: Number, require: true },
invoiceno: { type: String, require: true },
description: { type: String, require: true },
date: { type: String, require: true },
INR: { type: Number, require: true },
USD: { type: Number, require: true },
remarks: { type: String, require: true },
});
调用
.find().exec()
的结果是mongoose文档,如果生成的数据类型不符合架构约束,则不允许修改其值
但是,您可以在mongoose文档上使用.lean()
(请参阅),将其转换为普通js对象,然后可以任意修改:
let records = await Expense.find({}).exec().lean();
这是mongoQuery吗?你能给我们看一下你的支出模式吗?是的,使用mongoose检索对象数组。你可以像这样尝试
.lean()
,let records=wait Expense.find({}).lean().exec()代码>@Ifaruki Omg Thaks它可以工作,只是知道查询返回Mongoose文档类,而Mongoose文档类通常认为它只是对象