Next.js Discord

Discord Forum

MongoDB issue with Next.js

Answered
Spotted Redshank posted this in #help-forum
Open in Discord
Spotted RedshankOP
Warning: Only plain objects can be passed to Client Components from Server Components. Objects with toJSON methods are not supported. Convert it manually to a simple value before passing it to props.
{: {$__: ..., $isNew: false, _doc: ..., bot-id: ..., avatar: ..., username: ..., discriminator: ..., bio: ..., guilds: ..., type: ..., status: ..., stream-activity: ..., activity: ..., owner-id: ..., activity-interval: ..., activity-interval-enabled: ..., token: ...}}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning: Only plain objects can be passed to Client Components from Server Components. Objects with toJSON methods are not supported. Convert it manually to a simple value before passing it to props.
{_id: {}, bot-id: ..., avatar: Null, username: ..., discriminator: ..., bio: ..., guilds: ..., type: ..., status: ..., stream-activity: ..., activity: ..., owner-id: ..., activity-interval: ..., activity-interval-enabled: ..., token: ..., createdAt: ..., updatedAt: ...}
Answered by Spotted Redshank
export async function getVariablesByBotId(botId: string) {
    await connectMongoDB();
    let result = await Variable.find({'bot-id': botId});
    result[0]['_id'] = result[0]['_id'].toString();
    return result;
}


    const data = await getVariablesByBotId(botId);
    console.log(data);
View full answer

12 Replies

Spotted RedshankOP
export async function getBotById(botId: string) {
    await connectMongoDB();
    return await Bot.findOne({ 'bot-id': botId });
}
My website keeps spamming that warning, I'v tried hiding and setting th _id to a string but it does not fix it.
If you're replacing _id with just a plain string, then something else in that object must be a method or a constructor. Can you try printing the object to the console? Whichever field isn't a string or number needs to be stripped/serialized
yeah looks like it. try assigning _id.toString() to _id before returning the result
Spotted RedshankOP
  12 |     await connectMongoDB();
  13 |     let result = await Variable.find({'bot-id': botId});
> 14 |     result['_id'] = result['_id'].toString();
     |                                  ^
  15 |     return result;
  16 | }
 ⨯ TypeError: Cannot read properties of undefined (reading 'toString')
Nvm forgot it was an array
However that did not fix the issue, it also still shows as a "new ObjectId"
Spotted RedshankOP
export async function getVariablesByBotId(botId: string) {
    await connectMongoDB();
    let result = await Variable.find({'bot-id': botId});
    result[0]['_id'] = result[0]['_id'].toString();
    return result;
}


    const data = await getVariablesByBotId(botId);
    console.log(data);
Answer
did you fix it?
Spotted RedshankOP
Yep it did not work because mongodb objects are complex so had to convert it to a normal object