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
.