I have the below code for a DELETE
route on my app’s server. I’m facing two majors problems from this
:
1. Unable to remove item from array
This section of the code is supposed to remove friend
from friendsArray
. But it doesn’t work as intended. The output of console.log("Modified Friends Array",friendsArray)
is:
Modified Friends Array [
'friend1',
'friend2',
'friend3'
]
The value for friend
was given as friend2
in the query parameters. Which still exists even after the given instructions:
// removing friend from array
const friendsArray = await fetchFriends(email);
console.log("Friends array: "+friendsArray)
console.log("Friends array type: "+ typeof friendsArray)
console.log("Friend to be removed: "+friend)
delete friendsArray[friend]
JSON.stringify(friendsArray)
console.log("Modified Friends Array",friendsArray)
Note: the output of console.log("Friends array type: "+ typeof friendsArray)
is Friends array type: object
2. googleapis error while updating values
I receive this error from googleapis:
error: undefined,
status: 400,
code: 400,
errors: [
{
message: 'Invalid values[2][0]: list_value {n' +
' values {n' +
' string_value: "friend1"n' +
' }n' +
' values {n' +
' string_value: "friend2"n' +
' }n' +
' values {n' +
' string_value: "friend3"n' +
' }n' +
'}n',
domain: 'global',
reason: 'badRequest'
}
]
}
I tried setting the updating values from values: [[friendsArray]]
to values: [friendsArray]
and even values: [[[friendsArray]]]
but that didn’t work either…
Here is my entire file:
require("dotenv").config()
const axios = require('axios')
const { google } = require("googleapis")
const sheets = google.sheets({ version: 'v4' })
const creds = './credentials.json'
module.exports = {
route: "users/profile/friends",
method: "DELETE",
run: async (req, res) => {
async function fetchFriends(email) {
try {
const response = await axios.get(require('../api')('Profiles'))
const [headerRow, ...rows] = response.data.values;
const Data = rows.map(row => {
const obj = {};
headerRow.forEach((key, index) => {
obj[key] = row[index];
});
return obj;
});
const result = Data.filter(user => user.email === email)
return JSON.parse(result[0].friends)
} catch (e) {
console.log(e)
throw e
}
}
const { email, friend } = req.query
try {
// removing friend from array
const friendsArray = await fetchFriends(email);
console.log("Friends array: "+friendsArray)
console.log("Friends array type: "+ typeof friendsArray)
console.log("Friend to be removed: "+friend)
delete friendsArray[friend]
JSON.stringify(friendsArray)
console.log("Modified Friends Array",friendsArray)
const auth = await google.auth.getClient({
keyFile: creds,
scopes: ['https://www.googleapis.com/auth/spreadsheets']
})
const spreadsheetId = process.env.DATABASE_ID
const rowReq = {
spreadsheetId,
range: 'Profiles!A:D',
auth
}
const rowRes = await sheets.spreadsheets.values.get(rowReq)
const rows = rowRes.data.values
const rowIndex = rows.findIndex(row => row[0] === email)
const Index = rows[0].indexOf("friends")
const Column = String.fromCharCode(Index + 65)
const Range = `Profiles!${Column}${rowIndex + 1}`
const frndsUpdateRequest = {
auth,
spreadsheetId,
range: Range,
valueInputOption: 'RAW',
resource: {
values: [[friendsArray]],
},
}
const response = await sheets.spreadsheets.values.update(frndsUpdateRequest)
res.status(200).send(response)
} catch (e) {
console.log("Error removing friend: ", e)
res.status(500).send("Error removing friend: " + e.message);
}
}
}