Hi
Here’s my netlify site name : vpawards-vue
I’ve just got started with netlify cli and am using netlify dev to test my site locally… am very excited as I want to be able to test the serverless functions locally… so I did everything based on the requirements for installing the cli and added the site id with netlify link and everything works fine…:-)…
Except for the netlify functions… which seems to give me the error below… it’s a simple function that’s connecting to a mongo client and saving a document.
{"level":"error","message":"End - Error:"}
{"errorMessage":"Task timed out after 10.00 seconds","errorType":"TimeoutError","level":"error","stackTrace":["new TimeoutError (C:\\Users\\alimb\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lib\\utils.js:112:28)","Context.<anonymous> (C:\\Users\\alimb\\AppData\\Roaming\\npm\\node_modules\\netlify-cli\\node_modules\\lambda-local\\build\\lib\\context.js:110:19)","listOnTimeout (node:internal/timers:557:17)","processTimers (node:internal/timers:500:7)"]}
Response with status 500 in 10434 ms.
On the browser’s console I get this error
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
I am not sure how else to describe the error in detail… except that it works fine on the live site. If there’s anything more I can provide to you, I’d be happy to. Could you please help me with this so that I can start using netlify locally?
Here’s the function code that’s causing the issue locally but not on the live site…
const dotenv = require('dotenv')
dotenv.config()
const { MongoClient, ServerApiVersion } = require('mongodb')
const DB = process.env.VUE_APP_DATABASE.replace(
'{%PASSWORD%}',
process.env.VUE_APP_DATABASE_PASSWORD
)
const client = new MongoClient(DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
})
exports.handler = async function (event, context) {
const user = JSON.parse(event.body)
if (user._id) user._id = null
console.log('user', user)
await client.connect()
const db = client.db()
try {
const newUser = await db.collection('users').insertOne({ ...user })
client.close()
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
status: 'success',
user,
newUserId: newUser?.insertedId.toString(),
}),
}
} catch (error) {
console.log(error)
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
status: 'fail',
message: error.message,
error,
}),
}
}
}
Regards,
Alim