亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

nodejs objects 屬性只能更改為 int 而不是 string

nodejs objects 屬性只能更改為 int 而不是 string

慕娘9325324 2022-10-27 16:01:12
我剛剛遇到了這個奇怪的問題,其中下面的代碼按預期返回數組中所有對象的 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.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 },});
查看完整描述

1 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

.find().exec()調用的結果是貓鼬文檔,如果結果數據類型不符合模式約束,則不允許修改其值。

但是,您可以在 mongoose 文檔上使用.lean()(請參閱https://mongoosejs.com/docs/api.html#query_Query-lean),它將其轉換為普通的 js 對象,然后您可以任意修改:

let records = await Expense.find({}).exec().lean();


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號