Loopback: PersistedModel.updateAll Where filter does not work

Created on 29 Dec 2015  路  21Comments  路  Source: strongloop/loopback

This does not work.

var whereFilter = {}
whereFilter.where = {'or': [ {'managerId': 'x001'},  {'managerId': 'x002'} ] }

Employee.updateAll(whereFilter, {managerId: 'x003'}, function(err, info) {
    ...
});
stale

Most helpful comment

Update all does not have the "where" option,
you need to send only the query

ModelName.updateAll({
   and: [{
          endDate: {
            lt: new Date()
          }
        },{
          status: 'ACTIVE'
        }]
}, (err, instances) => ...

All 21 comments

What connector are you using?

mongo

Can you provide a sample repo for me to reproduce the issue?

@mike-aungsan Are you still running into issues? Is it working for you?

yes still having the same issue. Probably in a few weeks, I will setup a test repo and submit code.
Many Thanks

Hello. I am also getting the same problem with updateAll with a mongo data source.

{ "error": { "name": "AssertionError", "status": 500, "message": "The where argument must be an object", "actual": false, "expected": true, "operator": "==", "generatedMessage": false, "stack": "AssertionError: The where argument must be an object\n at Function.DataAccessObject.update.DataAccessObject.updateAll (/home/ec2-user/import_tool/node_modules/loopback-datasource-juggler/lib/dao.js:2121:3)\n at SharedMethod.invoke (/home/ec2-user/import_tool/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:248:25)\n at HttpContext.invoke (/home/ec2-user/import_tool/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:382:12)\n at phaseInvoke (/home/ec2-user/import_tool/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:645:9)\n at runHandler (/home/ec2-user/import_tool/node_modules/loopback/node_modules/loopback-phase/lib/phase.js:130:5)\n at iterate (/home/ec2-user/import_tool/node_modules/loopback/node_modules/async/lib/async.js:146:13)\n at Object.async.eachSeries (/home/ec2-user/import_tool/node_modules/loopback/node_modules/async/lib/async.js:162:9)\n at runHandlers (/home/ec2-user/import_tool/node_modules/loopback/node_modules/loopback-phase/lib/phase.js:139:13)\n at iterate (/home/ec2-user/import_tool/node_modules/loopback/node_modules/async/lib/async.js:146:13)\n at /home/ec2-user/import_tool/node_modules/loopback/node_modules/async/lib/async.js:157:25" } }

Is this a bug in loopback? or in the mongo datasource?

This would work from the Node API. For updates, the "where" property is omitted. BUT How do I do this omission from the REST API?

var whereFilter = {}
whereFilter= {'or': [ {'managerId': 'x001'},  {'managerId': 'x002'} ] }

Employee.updateAll(whereFilter, {managerId: 'x003'}, function(err, info) {
    ...
});

What is the status of this issue? I am having the same problem with mysql connector. I need to use and operator.

Can someone can provide a sample repo for me to reproduce the issue? See https://github.com/strongloop/loopback/wiki/Reporting-issues#bug-report

fe:

    Subscription.updateAll({
      where: {
        and: [{
          endDate: {
            lt: new Date()
          }
        },{
          status: 'ACTIVE'
        }]
      }
    },{
      status: 'EXPIRED'
    }).then(function (info) {
      console.log('Subscription.status: updated %d', info.count);
      done();
    }).catch(function (err) {
      console.error('Subscription.status: error', err);
      done(err);
    });

Subscription.status: error { MongoError: unknown operator: $and
at Function.MongoError.create (/home/cojack/Projects/subscription-api/node_modules/mongodb-core/lib/error.js:31:11)
at toError (/home/cojack/Projects/subscription-api/node_modules/mongodb/lib/utils.js:114:22)
at Object.cb (/home/cojack/Projects/subscription-api/node_modules/mongodb/lib/collection.js:1014:67)
at /home/cojack/Projects/subscription-api/node_modules/mongodb-core/lib/connection/pool.js:436:18
at authenticateStragglers (/home/cojack/Projects/subscription-api/node_modules/mongodb-core/lib/connection/pool.js:392:16)
at .messageHandler (/home/cojack/Projects/subscription-api/node_modules/mongodb-core/lib/connection/pool.js:414:5)
at Socket. (/home/cojack/Projects/subscription-api/node_modules/mongodb-core/lib/connection/connection.js:294:22)

MiniMongo doesn't like $where, $and or $or clauses

@superkhau, I've done repo to reproduce the error .

OS => Ubuntu 16.04
NodeJS => v6.10.3
NPM => 3.10.10
MySQL => mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using EditLine wrapper

Error log

{"error":{"name":"AssertionError","status":500,"message":"The where argument must be an object","actual":false,"expected":true,"operator":"==","generatedMessage":false,"stack":"AssertionError: The where argument must be an object\n at Function.DataAccessObject.update.DataAccessObject.updateAll (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-datasource-juggler/lib/dao.js:2554:3)\n at SharedMethod.invoke (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/shared-method.js:263:25)\n at HttpContext.invoke (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/http-context.js:389:12)\n at phaseInvoke (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/remote-objects.js:654:9)\n at runHandler (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/lib/phase.js:135:5)\n at iterate (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n at Object.async.eachSeries (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9)\n at runHandlers (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/lib/phase.js:144:13)\n at iterate (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n at /mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:157:25\n at /mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25\n at execStack (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/remote-objects.js:493:7)\n at RemoteObjects.execHooks (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/remote-objects.js:497:10)\n at phaseBeforeInvoke (/mnt/Projects/Web/project/loopback-sandbox/node_modules/strong-remoting/lib/remote-objects.js:650:10)\n at runHandler (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/lib/phase.js:135:5)\n at iterate (/mnt/Projects/Web/project/loopback-sandbox/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)"}}

How to setup app

  1. You need to setup mysql database and update credentials into datasources.json file for mysqlDs
  2. Run commands npm install and bower install
  3. Run SEED_DB='dev' node server/server.js to migrate models and insert initial data
  4. node server/server.js to run project
  5. Open browser (http://localhost:3000/) console to see the error

I am also facing the same issue with updateAll method:

Unhandled error for request POST /api/Shifts/update?_id=598ee8d2c5f01f26d4fba397: AssertionError: The where argument must be an object
at Function.DataAccessObject.update.DataAccessObject.updateAll (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\loopback-datasource-juggler\lib\dao.js:2656:3)
at SharedMethod.invoke (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\shared-method.js:270:25)
at HttpContext.invoke (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\http-context.js:297:12)
at phaseInvoke (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\remote-objects.js:677:9)
at runHandler (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\lib\phase.js:135:5)
at iterate (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:146:13)
at Object.async.eachSeries (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:162:9)
at runHandlers (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\lib\phase.js:144:13)
at iterate (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:146:13)
at C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:157:25
at C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:154:25
at execStack (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\remote-objects.js:522:7)
at RemoteObjects.execHooks (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\remote-objects.js:526:10)
at phaseBeforeInvoke (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\lib\remote-objects.js:673:10)
at runHandler (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\lib\phase.js:135:5)
at iterate (C:\SOFT\JavascriptStacks\LoopBack\LB_HelloWorld\loopback-example-angular\node_modules\strong-remoting\node_modules\loopback-phase\node_modules\async\lib\async.js:146:13)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

How can you close this issue? The bug is still there! +1

Update all does not have the "where" option,
you need to send only the query

ModelName.updateAll({
   and: [{
          endDate: {
            lt: new Date()
          }
        },{
          status: 'ACTIVE'
        }]
}, (err, instances) => ...

@madkoding how this fix works on the rest endpoint?

fetch( serverUrl + '/endpoint/update?filter[where][videoID]=' + thisID + '&access_token=' + userToken, {

method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
closeddate: new Date()
})
})

trows: AssertionError [ERR_ASSERTION]: The where argument must be an object

What is the way to update the instances which satisfy a particular condition if updateAll doesn't accept a where filter?

return this.policyApi
        .updateAll( {and: [{id : "5c0fd94e5c4071220cf05d06" }]}, policy)
        .pipe(
          mergeMap((data: any) => [
            new SubmitPolicyCompleteAction(data.submissionResponse),
            new FetchPoliciesAction()
          ])
        );

Provisional headers are shown
Accept: application/json, text/plain, /
.
.
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/71.0.3578.80 Chrome/71.0.3578.80 Safari/537.36
where: {"and":[{"id":"5c0fd94e5c4071220cf05d06"}]}

...internal server error:
Unhandled error for request POST /api/v2/Policies/update: AssertionError [ERR_ASSERTION]: The where argument must be an object
at Function.DataAccessObject.update.DataAccessObject.updateAll (/home/enc

you don't need to use where class here, you can directly use

 Employee.updateAll({ employee: 'test' }, { age: 32 }, function(err, data) {})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cajoy picture cajoy  路  4Comments

htmlauthor picture htmlauthor  路  3Comments

devotox picture devotox  路  4Comments

Overdrivr picture Overdrivr  路  4Comments

ian-lewis-cs picture ian-lewis-cs  路  4Comments