When attempting to create an associated table of tags with the id of a tag and the id of a related article, I encountered an issue. Despite successfully using the findOrCreate function to create a tag, the resulting id was null when trying to use it in my association with the tag's id as result.id. Strangely, while a simple create operation returns an id, findOrCreate does not. How can I retrieve the id of an entry created with the findOrCreate function? Additionally, I am open to exploring alternative methods for creating entries that do not already exist. Below is the code snippet from my app.js:
function(sales, callback) {
if(req.body.tags) {
// Code block
}
}
Here is the tag controller:
module.exports = {
create(req, res, tag) {
return Tags
.findOrCreate({
// Code block
})
}
};
And here is the tag model:
module.exports = (sequelize, DataTypes) => {
// Code block
}
After further research and testing, I tried the following approach in my tag controller:
module.exports = {
findOrCreate(req, res, tag) {
return Tags
.findOrCreate({
// Code block
}).spread((tags, created) => {
console.log(tags.get({
plain: true
}))
})
}
};
While this successfully logs all created tags, I am facing difficulties in retrieving their ids. When attempting to return the ids instead of logging them, I only receive false results for tags that already exist in the database. I am currently stuck and seeking assistance on resolving this issue.