草庐IT

node.js - Mongoose Schema.update 不更新 bool 值

coder 2023-10-30 原文

我已经尝试更新其他字段并且它工作得很好。

我在 API 中使用的命令:

  User.update({ email: targetUser.email }, { $set: { isAdmin: true }, $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

澄清一下,当我尝试运行它时,API 返回代码 200,这意味着一切正常,但当我检查数据库时,isAdmin 值没有改变。

任何建议都会有所帮助,这里没有想法!

请求的用户模式:

var UserSchema = new Schema({
  name: { type: String, default: "", index: 'text' },
  email: { type: String, lowercase: true },
  role: { type: String, default: "" },
  meta: {
    skills: { type: Array, default: [], index: 'text' },
    about: { type: String, default: "", index: 'text' },
    education: { type: Array, default: [], index: 'text' },
    location: {
      address: {
        a: { type: String, default: "" },
        p: { type: String, default: "" },
        c: { type: String, default: "" }
      },
      geo: {
        lat: { type: Number, default: 0 },
        lng: { type: Number, default: 0 }
      }
    }
  },

  compMeta:
  {
    departments: { type: Array, default: [], index: 'text' },
    employees:
    [
      {
        emId: Number,
        empName: String,
        empDep: String  // Dunno if i should use Dep name or Dep ID gonna look in to that later
      }
    ],
  }
  ,
  settings: {
    search: {
      distance: {
        n: { type: Number, default: 100 },
        t: { type: String, default: "km" }
      }
    }
  },
  created: {
    type: Date,
    default: Date.now
  },
  //Rating is an array of objects that consist of rateing 0-100 , job database id , comments from the Company
  rating:
  [
    {
      rate: Number,
      jobId: Number,
      jobComments: String
    }
  ],
  /*rating:
  {
    userTotalRating: {type: Number, default: 0},
    ratingCounter : {type: Number, default: 0}
  }*/


  sensitive: {
    cpr_cvr: String,
  },
  stripe: { type: String },
  facebook: {},
  linkedin: {},
  log: {
    updated: { type: Array, default: [] }
  },
  hashedPassword: String,
  provider: { type: String, default: 'local' },
  salt: String
});

更新:

Mongodb 版本:3.0.7

最佳答案

原来我只是忘了将 isAdmin 字段添加到我的用户架构中!还有,我调用update是错的,我改成这样:

User.update({ email: targetUser.email }, { $set: { isAdmin: true }}, { $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

感谢所有努力帮助我的人! :)

关于node.js - Mongoose Schema.update 不更新 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33997078/

有关node.js - Mongoose Schema.update 不更新 bool 值的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  5. ruby - 导轨 4 : column reference "updated_at" is ambiguous with Postgres - 2

    我正在尝试使用“updated_at”字段的日期时间范围查询数据库。前端在JSON数组中发送查询:["2015-09-0100:00:00","2015-10-0223:00:00"]在RailsController中,我使用以下方法将两个字符串解析为DateTime:start_date=DateTime.parse(params[:date_range_arr][0])end_date=DateTime.parse(params[:date_range_arr][1])#...@events=@events.where('updated_atBETWEEN?AND?,start_d

  6. ruby-on-rails - Rails Associations 的更新方法是什么? - 2

    这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user

  7. ruby - Ruby 中的 bool 方法? - 2

    为了问这样的问题:MyClass::create().empty?如何在MyClass中设置empty?Empty(true/false)取决于类变量@arr是否为空。 最佳答案 问号实际上是方法名称的一部分,因此您可以这样做:classMyClassdefempty?@arr.empty?#Implicitlyreturned.endend 关于ruby-Ruby中的bool方法?,我们在StackOverflow上找到一个类似的问题: https://st

  8. ruby-on-rails - OSX Yosemite 更新破坏了 pow.cx - 2

    升级到OSXYosemite后,我现有的pow.cx安装不起作用。升级到最新的pow.cx无效。通过事件监视器重新启动它也没有成功。 最佳答案 卸载(!)并重新安装解决了这个问题。curlget.pow.cx/uninstall.sh|shcurlget.pow.cx|sh 关于ruby-on-rails-OSXYosemite更新破坏了pow.cx,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q

  9. ruby - 将 Gitlab 从 9.3.7 更新到 9.3.8 安装 re2 时出错 - 2

    我们在Ubuntu14.04和Gitlab9.3.7上运行,运行良好。我们正在尝试更新到Gitlabv9.3.8的最新安全补丁,但它给我们这个错误:Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension.currentdirectory:/home/git/gitlab/vendor/bundle/ruby/2.3.0/gems/re2-1.0.0/ext/re2/usr/local/bin/ruby-r./siteconf20170720-19622-15i0edf.rbextconf.rbcheckingformain(

  10. ruby-on-rails - Rails 更新属性 - 2

    我遇到了以下问题。我有一个名为user的模型,它有一个名为activated的列。我试图通过激活的方法更新该值?但它给我错误:验证失败:密码不能为空,密码太短(最少6个字符)这对我来说没有意义,因为我没有接触密码字段!我只想更新激活的列。我把我认为相关的代码放在这里,但如果你认为你需要更多,请问:)非常感谢您!型号:attr_accessor:passwordattr_accessible:name,:email,:password,:password_confirmation,:activatedhas_many:sucu_votesemail_regex=/\A[\w+\-.]+@

随机推荐