i'm trying to update record if exist else i want to create new record, i tried findById function but when i try to add new record inside it present this error : create() is not a function .. here is my code :
Userpost.isMyPost = function (data , req, cb) {
Userpost.findById(data.id, (err, Userpost) => {
if (!err && Userpost) {
Userpost.create({
U_ID: "1" ,
imageUrl: "ss" ,
userLike : "aa" ,
sharedFrom: "ss",
isDeleted : false ,
isFav : true
}, function(err, Userpost) {
cb(err , Userpost);
});
} else {
cb (err, {});
}
});
}
Looks like you've overridden Userpost
model with the returned data-value, so you don't have .create()
and all other methods on it.
Try to replace (err, Userpost) => ...
with some difference variable name e.g. (err, response) => ....
in your findById
callback function:
Example:
Userpost.isMyPost = function (data , req, cb) {
Userpost.findById(data.id, (err, response) => {
if (!err && response) {
Userpost.create({
U_ID: "1" ,
imageUrl: "ss" ,
userLike: "aa" ,
sharedFrom: "ss",
isDeleted: false ,
isFav: true
}, function(err, response) {
cb(err, response);
});
} else {
cb(err, {});
}
});
}