0

I'm working with Node.js and I'm using the Cloudant connector for the data source of loopback.

I have defined a model called User as defined below:

var User = db.define('User', {
  lastName: { type: String },
  firstName: { type: String },
  skills: []
});

I have an instance of this model like:

{
    "lastName": "Doe",
    "firstName": "John",
    "skills": [
      {
        "id": "0",
        "text": "JAVA"
      },
      {
        "id": "1",
        "text": "CSS"
      },
      {
        "id": "2",
        "text": "HTML"
      },
      {
        "id": 3,
        "text": "JAVASCRIPT"
      },
      {
        "id": 4,
        "text": ".NET"
      }
    ],
    "id": "d981b42c3a2a13da382102c76652b96e"
  }

I want to update the instance so that the skills array only contains this information:

"skills": [
      {
        "id": "0",
        "text": "JAVAFX"
      },
      {
        "id": "1",
        "text": "CSS3"
      }
    ]

I want to use this function:

app.post('/updateProfile', function(req, res) {
  User.updateAll(
    {_id:req.body.id}, 
    {skills: req.body.skills},
    function(err,items){
    if(err){
      res.send(err);
    } else {
      res.sendStatus(200);
    }
  });
});

However, the problem that I face is that only the skills with the same ID are updated. The ones that I want to be deleted (in the example it's HTML, JAVASCRIPT, and .NET) are NOT deleted.

How can I actually replace the skills instead of just doing an update? I've tried some other things that were described in the documentation, but it's not working. Documentation is available here

JustAnotherDev
  • 445
  • 1
  • 4
  • 20

1 Answers1

1

Confirmed that it's a bug in loopback-connector-cloudant. Filed a bug for it: https://github.com/strongloop/loopback-connector-cloudant/issues/44

Other loopback datasource like "memory" will replace the whole skills object, which works fine.

As a workaround, there is a methond User.replaceById(id, {data}, cb) you can use to replace the whole skill property. The patch is still under verify, hasn't released yet, I will update here as long as it's published: https://github.com/strongloop/loopback-connector-cloudant/pull/34

So in your case, it should be

User.replaceById('d981b42c3a2a13da382102c76652b96e', { "lastName": "Doe", "firstName": "John", "skills": [ { "id": "0", "text": "JAVAFX" }, { "id": "1", "text": "CSS3" } ] }, function(err, replacedInstance){ // get your replaced instance here })

A limitation of this method is you have to include unchanged properties as well in your update data, like lastName, firstName.

Janny Hou
  • 121
  • 3
  • essage : "The connector cloudant does not support replaceById operation. This is not a bug in LoopBack. Please contact the authors of the connector, preferably via GitHub issues. – JustAnotherDev Oct 19 '16 at 07:56
  • Thank you for reminding me: The patch is still under verify, hasn't released yet, I will update here as long as it's published: https://github.com/strongloop/loopback-connector-cloudant/pull/34 – Janny Hou Oct 19 '16 at 20:51