strongloop / loopback

LoopBack makes it easy to build modern applications that require complex integrations.
http://loopback.io
Other
13.23k stars 1.2k forks source link

I can't believe I can't figure out how to update a record! #899

Closed dstroot closed 9 years ago

dstroot commented 9 years ago

I'd really appreciate a nudge in the right direction. Say I have a model called 'widgets' with a property 'name'. I create a widget with name "mygoodwidget".

Now I want to update the name of the widget to 'myGREATwidget'.

I know the id of the widget. Say the id is '1'.

I am in nodejs - How do I make this happen? I'd GREATly appreciate a bit of help.

code I've tried:

// model to use
var widget = app.models.widget;

// Use the model for CRUD
var widgetInstance = new widget();

widgetInstance.setId(widget.id);
widgetInstance.name = 'something else';
widgetInstance.save(function (err) {
  if (err) {
   debug('Error: ' + err);
  }
});

widgetInstance.updateAttribute(name, 'something else', function (err) {
  if (err) {
    debug('Error: ' + err);
  }
});

// where filter
var whereFilter = {
  where: {
    Id: widget.id
  }
};

widget.findOne(whereFilter, function (err, foundwidget) {
  if (err) {
    debug('Error: ' + err);
  }
  if (foundwidget) {
    foundwidget.name = 'something else';
    foundwidget.save(function (err) {
      if (err) {
       debug('Error: ' + err);
      }
    });
  } else {
    debug('widget not found!');
  }
});
raymondfeng commented 9 years ago

Do you intend to call save/updateAttribute/findOne in serial? If so, your code has serious flaw. You either have to call updateAttribute inside the callback of save, and findOne inside the callback of updateAttribute.

This article will help you to manage the flow: http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/ http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/.

Thanks,


Raymond Feng Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop http://strongloop.com/ makes it easy to develop APIs http://strongloop.com/mobile-application-development/loopback/ in Node, plus get DevOps capabilities http://strongloop.com/node-js-performance/strongops/ like monitoring, debugging and clustering.

On Dec 5, 2014, at 1:24 PM, Dan Stroot notifications@github.com wrote:

I'd really appreciate a nudge in the right direction. Say I have a model called 'widgets' with a property 'name'. I create a widget with name "mygoodwidget".

Now I want to update the name of the widget to 'myGREATwidget'.

I know the id of the widget. Say the id is '1'.

I am in nodejs - How do I make this happen? I'd GREATly appreciate a bit of help.

code I've tried:

// model to use var widget = app.models.widget;

// Use the model for CRUD var widgetInstance = new widget();

widgetInstance.setId(widget.id); widgetInstance.name = 'something else'; widgetInstance.save(function (err) { if (err) { debug('Error: ' + err); } });

widgetInstance.updateAttribute(name, 'something else', function (err) { if (err) { debug('Error: ' + err); } });

// where filter var whereFilter = { where: { Id: widget.id } };

// get the linked provider widget.findOne(whereFilter, function (err, foundwidget) { if (err) { debug('Error: ' + err); } if (foundwidget) { foundwidget.name = 'something else'; foundwidget.save(function (err) { if (err) { debug('Error: ' + err); } }); } else { debug('widget not found!'); } }); — Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/899.

dstroot commented 9 years ago

Raymond - thanks for responding so quickly. I understand callbacks - I don't intend all these to be serial. The code I posted was just an example of stuff I have tried. I am struggling with loopback a bit. I was hoping for some example code that simply updates a property on a record. This has got to be pretty basic right? "get a record" > "update the record"...

raymondfeng commented 9 years ago

What problems do you see? This code seems to be suspicious:

// where filter
var whereFilter = {
  where: {
    Id: widget.id
  }
};

The Id is probably id.

For references to updating data, see http://docs.strongloop.com/display/LB/Creating,+updating,+and+deleting+data#Creating,updating,anddeletingdata-Updatingdata(modelinstances)

dstroot commented 9 years ago

Great catch! This works: (note I added a property to the native User model)

// model to use
var User = app.models.User;

// where filter
var whereFilter = {
  where: {
    id: user.id
  }
};

// get the user
User.findOne(whereFilter, function (err, founduser) {
  if (err) {
    return debug('Error: ' + err);
  } else {
    if (founduser) {
      // update user's last logon
      founduser.last_logon = Date.now();
      founduser.save(function (err) {
        if (err) {
          return debug('Error: ' + err);
        }
      });
    } else {
      debug('User not found!');
    }
  }
});