I am trying to use the method Model.findOrCreate in loopback using the mongodb connector
Country.findOrCreate({where: {iso2a: iso2a}}, {
"iso2a": iso2a,
"polygon": polygon
}, function(err, obj){
if(err){
console.log("Error finding and/or creating:", err);
}else{
obj.iso2a = iso2a;
obj.polygon = polygon;
obj.save(function(err, obj){
if(err){
console.log("Error saving");
}else{
console.log("Success saving");
}
});
}
});
But I keep getting the error that the function does not exists...
I guess I am doing something pretty basic wrong, ohh yeah and I checked that the model is "loaded". Thanks.
I've read the docs here for PersistedModel.findOrCreate(where, data, callback). Now you see the first argument only accepts where
clause, so you don't have to specify it explicitly. Here's the corrected code:
Country.findOrCreate(
{ iso2a: iso2a }, //adding where clause is not required.
{
"iso2a": iso2a,
"polygon": polygon
},
function(err, obj) {
if(err) {
console.log("Error finding and/or creating:", err);
} else {
obj.iso2a = iso2a;
obj.polygon = polygon;
obj.save(function(err, obj) {
if(err) {
console.log("Error saving");
} else {
console.log("Success saving");
}
});
}
});
Hope it solves your problem.