3

I'm new to using node-orm2 and am getting caught by the following issue;

TypeError: Object [object Object] has no method 'save'

Full stack trace http://pastebin.com/cb9Lt9pB

This error is coming from within a Model method, definition below;

User.findOrCreate = function (providerName, token, tokenSecret, uid, profile, done) {
this.find({uid: uid}, 1, function(err, user) {
  if (user) {

    user.displayName = profile.displayName;
    user.profileImage = profile._json.profile_image_url;

    user.save(function(err){
      return done(err, user, false);
    });

  } else {
    ....

The full code for the model definition is here http://pastebin.com/7Bv8XG36

From what I can tell the instance isn't being returned with a save method as outlined in the documentation (https://node-orm.readthedocs.org/en/latest/ref/instance.html).

If I perform a console.log on the user object you can clearly see it doesn't have any methods other than getters and setters.

[ { id: [Getter/Setter],
uid: [Getter/Setter],
username: [Getter/Setter],
displayName: [Getter/Setter],
token: [Getter/Setter],
tokenSecret: [Getter/Setter],
profileImage: [Getter/Setter] } ] 

Can anyone provide any insight to if I'm using this incorrectly? Or any pointers to help me understand why the save method isn't on the instance.

For reference I'm using the latest version of node-orm2 ("orm": "*") with mysql.

1 Answer 1

5

Even if you limit the number of results to 1, node-orm2 will still pass an array of results:

this.find({uid: uid}, 1, function(err, users) {
  if (! err && users.length) {
    var user = users[0]; // user.save() should work now
    ...
  }
});
1
  • yes, and use model.get(someId) if you know the primary id of what you are trying to retrieve (uid may or may not be the key in the original post).
    – akaphenom
    Commented May 3, 2014 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.