0

I am using loopback. I wish to create record if it doesn't exist. I used Modal.findOrCreate method to maintain uniqueness but its showing me error of findOrCreate is not a function but Docs show it is. What I tried was.

Poet.findOrCreate({where: {name: query.name}}, {name:query.name},function(response){
         console.log(response);
        });

I am editing question writing complete function what I have written.

    $scope.poetAdded = function(query) {
        if (!angular.isUndefinedOrNull(query.id)) {
            //          console.log("itemId : " + $scope.items[$scope.editIndex].id);
            if (!angular.isUndefinedOrNull($scope.form.id)) {
                Song.updateAll({
                        where: {
                            id: $scope.form.id
                        }
                    }, {
                        poetId: query.id
                    })
                    .$promise.then(function(value) {
                        $scope.form.poetId = query.id;
                        $scope.items[$scope.editIndex].poetId = query.id;
                    }, function(reason) {
                        console.log("error :" + JSON.stringify(reason.data.error.details.messages));
                        console.log(reason.data.error.details.messages);
                    });
            } else {
                $scope.form.poetId = query.id;
            }
        } else {
            console.log("find & Create function to be used !!");
            Poet.findOrCreate({
                where: {
                    name: query.name
                }
            }, {
                name: query.name
            }, function(err, instance) {
                if (err) console.log(err);
                console.log(instance); //instance found or created
            });
        }
    };

My else condition is failing on findOrCreate.

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Sankalp
  • 1,300
  • 5
  • 28
  • 52
  • That code should work. Your problem is probably with your model. can you provide some details? Second thing, callback takes two arguments error and instance (see docs). "Response" that you are writing to console will be error object. Should be something like function(err, instance){ if (err) console.log(err); console.log(instance); //instance found or created } – A.Z. May 19 '15 at 07:57
  • I haven't written any code in Model. Its completely blank. Since its inbuilt function I think it should work. I simply have created model and relate it with MySQL. Thats it. – Sankalp May 19 '15 at 13:32
  • That is true, this method is a member of PersistedModel, that is why I assume that your variable "Poet" does not contain model. Can you tell me where did you put this code? – A.Z. May 19 '15 at 13:37
  • I put this code in Angular COntroller taking dependency of Poet Model – Sankalp May 19 '15 at 17:24
  • When I checked lb-service file, its not mentioned in that file also. – Sankalp May 19 '15 at 17:43

1 Answers1

4

Based on our conversation, error message and your last comment, your problem is that you mixed loopback model API (server side API) and loopback angularjs SDK (client side). Loopback model API and client API are slightly different. Like error message suggests findOrCreate method does not exists on the client PersistedModel.

On the client side you can use upsert method, or you can check does record exists and then based on a result you can do insert. Check the docs about client side API's

http://apidocs.strongloop.com/loopback-sdk-angular/#persistedmodel-upsert

UPDATE

Loopback is a framework based on nodejs. You use loopback to easily create REST API's. If you want to interact with data you do that by using built in models or creating your own. This works on a server side and this is nodejs application.

On a client side your data are accessible via REST API. You can use HTTP requests to CRUD data from any application that is capable of making http request. Now, what loopback also provides is a client side SDK to make things easier. When you used lb-ng command you have generated AngularJS services that interacts with loopback REST API's. This is a client angularjs application.

A.Z.
  • 1,638
  • 2
  • 18
  • 27
  • Sorry, but I am not clear as I am beginner. findOrCreate is NodeAPI right? Then where it will be used if not on Client API ?? – Sankalp May 19 '15 at 17:53
  • I will update my answer to make it more clear. Sorry if I confused you. – A.Z. May 19 '15 at 18:06