N
Novu4mo ago
AndreMaz

Missing _templateId causes `Response validation failed` when fetching notifications via @novu/api

I think I discovered a problem with the @novu/api lib When I run
import { Novu } from "@novu/api";

const novu = new Novu({
secretKey: "<KEY>",
serverURL: "<URL>",
});

const fetcher = async () => {
const subscriberId = "<ID>";

const notificationsRes = await novu.subscribers.notifications.feed({
subscriberId,
limit: 10,
page: 1,
});

return notificationsRes;
};

fetcher()
.then((notifications) => {
const {
result: { hasMore, pageSize, totalCount, page, data },
} = notifications;
console.log(
`Total count=${totalCount}, Page size=${pageSize}, Has more=${hasMore}, Current page=${page}`
);
console.log("Notification data:", data);
})
.catch((error) => {
console.error("Error fetching notifications:", error);
});
import { Novu } from "@novu/api";

const novu = new Novu({
secretKey: "<KEY>",
serverURL: "<URL>",
});

const fetcher = async () => {
const subscriberId = "<ID>";

const notificationsRes = await novu.subscribers.notifications.feed({
subscriberId,
limit: 10,
page: 1,
});

return notificationsRes;
};

fetcher()
.then((notifications) => {
const {
result: { hasMore, pageSize, totalCount, page, data },
} = notifications;
console.log(
`Total count=${totalCount}, Page size=${pageSize}, Has more=${hasMore}, Current page=${page}`
);
console.log("Notification data:", data);
})
.catch((error) => {
console.error("Error fetching notifications:", error);
});
I'm getting the following error:
// error.statusCode
200

// error.message
Response validation failed

// error.cause
{
issues: [
{
code: "invalid_type",
expected: "string",
received: "undefined",
path: [
"Result",
"data",
9,
"_templateId",
],
message: "Required",
},
],
}
// error.statusCode
200

// error.message
Response validation failed

// error.cause
{
issues: [
{
code: "invalid_type",
expected: "string",
received: "undefined",
path: [
"Result",
"data",
9,
"_templateId",
],
message: "Required",
},
],
}
Expected behaviour: Even if the _templateId is missing I still should be able to retrieve data. Bonus question: The data that comes from novu.subscribers.notifications.feed() is different from the one that comes from useNotifications() hook. Is there a way of getting InboxNotification (just as in useNotifications) via the @novu/api ?
8 Replies
AndreMaz
AndreMazOP4mo ago
Another issue: If I pass negative page size (e.g., -1) the request will hang forever
Pawan Jain
Pawan Jain4mo ago
@AndreMaz Could you please share the @novu/api version?
AndreMaz
AndreMazOP4mo ago
@Pawan Jain Apologies. Forgot to add this info. I'm on v1.4.0 @Pawan Jain another question. What's the purpose of payload in the request? Docs state
Base64 encoded string of the partial payload JSON object
but I don't understand the meaning of this. Cal you please clarify?
Pawan Jain
Pawan Jain3mo ago
@AndreMaz Could you please share if you are using novu framework? do you see any specific meessage with which _templateId error occurs?
What's the purpose of payload in the request?
Payload is an object which is used to send key value pairs to customize the notification content
AndreMaz
AndreMazOP3mo ago
Hey @Pawan Jain I'm on vacations right now. I'll check the templateId issue next week. I'll let you know when I have more info
Novu_Bot
Novu_Bot3mo ago
@AndreMaz, you just advanced to level 1!
AndreMaz
AndreMazOP3mo ago
@Pawan Jain I'm back.
Could you please share if you are using novu framework? do you see any specific meessage with which _templateId error occurs?
Tested again but the error is gone. Did you change something on server side?
Payload is an object which is used to send key value pairs to customize the notification content
Yep, it's similar to data in useNotifications hook in a sense that the payload can be used to filter/query the notifications. Probably the documentation can be improved as the Base64 encoded string of the partial payload JSON object is not explicit enough. Some additional questions: - Would it be possible, in future versions of @novu/api, to also filter by data ? - Can you take a look at https://github.com/novuhq/novu-ts/issues/78 ?
GitHub
[Feature Request] Allow archive notification via API · Issue #78 ...
At the moment it&#39;s not possible to archive the notification via API. Docs: https://docs.novu.co/api-reference/subscribers/update-notifications-state Only available states are &quot;read&quot; |...
Pawan Jain
Pawan Jain3mo ago
Would it be possible, in future versions of @novu/api, to also filter by data ?
We have added this option in @novu/react hooks https://docs.novu.co/platform/inbox/react/tabs#filter-tabs-by-data-attributes

Did you find this page helpful?