Server is established, connection to DB also, but ...
# support
j
I have connected a server (Render) to the Assistants (using the Account, functions, eval etc. just to make sure), the server says it is connected to MongoDB, but no data arrives at MongoDB. the app.js file is the following, maybe here is the mistake? require('dotenv').config(); const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); const app = express(); app.use(bodyParser.json()); mongoose.connect(process.env.DB_URI) .then(() => console.log('Connected to MongoDB')) .catch(err => console.error('Could not connect to MongoDB', err)); const UserSchema = new mongoose.Schema({ phoneNumber: { type: String, required: true, unique: true }, lastInteraction: Date, interactionHistory: [{ date: { type: Date, default: Date.now }, transcript: String, summary: String }] }); const User = mongoose.model('User', UserSchema); app.post('/webhook', async (req, res) => { console.log('Received webhook:', req.body); if (req.body.message && req.body.message.type === 'end-of-call-report') { const { call, transcript, summary } = req.body.message; try { let user = await User.findOne({ phoneNumber: call.from }); if (!user) { user = new User({ phoneNumber: call.from }); } user.lastInteraction = new Date(); user.interactionHistory.push({ transcript, summary }); await user.save(); console.log('Saved user data:', user); } catch (error) { console.error('Error saving user data:', error); } } res.sendStatus(200); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(
Server running on port ${PORT}
));
v
@Johannes The code you've provided for
app.js
seems to be set up correctly for the most part. However, there are a few things we can check to ensure that data is being sent and received as expected: 1. **Environment Variables**: Ensure that the
.env
file contains the correct MongoDB URI in the
DB_URI
variable. The URI should be in the format
mongodb+srv://<username>:<password>@<cluster-address>/<dbname>?retryWrites=true&w=majority
. 2. **Database Connection**: Verify that the MongoDB instance is accessible from the server where the application is running. Network policies or firewalls might be blocking the connection. 3. **Webhook Endpoint**: Make sure that the
/webhook
endpoint is being hit by the external service. You can add more logging to see if the requests are coming in. 4. **Data Format**: Confirm that the data being sent to the
/webhook
endpoint matches the expected format, particularly the
req.body.message
object structure. 5. **Error Handling**: The error handling in the catch block will log errors, but it might be helpful to send a response with the error message to the client for easier debugging. 6. **Mongoose Model**: Double-check that the
User
model schema matches the data you're trying to save. The
phoneNumber
field is marked as unique, so ensure that duplicate phone numbers aren't causing issues. 7. **Server Logs**: Review the server logs to see if there are any runtime errors or warnings that could indicate a problem. 8. **Database Logs**: Check the MongoDB logs to see if there are any access errors or other issues that might prevent data from being written. If you've checked all these points and still face issues, it might be helpful to add more detailed logging around the database connection and operations to pinpoint where the failure is occurring. If you need further assistance, please provide more details or error messages you're encountering.
You can continue this conversation by mentioning me in the message.
s
@Johannes did you set the server url in your assistant config?
j
Yes I did, seems super random when server is triggered.
s
No it's random please check the server events you selected. Also by any chance if you felt it is random then please share the assistantId, and your expectations with server_messages/server_events.
j
Yes resolved - The problem was that i was referring to a phone number instead of number in my code.
6 Views