Encountering a problem in a MEAN application while sending a PUT request

I am encountering an issue while developing a todos app in MEAN stack with MongoDB installed locally. The error I am facing is related to the PUT request. Any assistance on resolving this problem would be greatly appreciated.

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/bson/lib/bson/objectid.js:50:11)
    at Function.ObjectID (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/bson/lib/bson/objectid.js:31:42)
    at router.delete.db.todos.update._id (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:59:26)
    ...

...
ReferenceError: updObj is not defined
    at module.exports (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:73:8)
    ...

todos.js (Server side)

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb://user1:user@localhost/meantodo', ['todos']);

router.get('/todos', function (req, res, next) {
   ...
});

...

todos.component.html

<div class="add-todo-form text-center">
    <h1>Add Todo</h1>
    ...

todos.component.ts

import { Component, OnInit } from '@angular/core';
import { TodoService } from '../services/todo.service';
import {Todo} from '../Todo';

@Component({
    moduleId: module.id,
    selector: 'todos',
    templateUrl: 'todos.component.html',
})

...

todo.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class TodoService {
    constructor(private _http: Http) {
    }

    getTodos() {
        ...
    }

    saveTodo(todo){
        ...
    }

    updateTodo(todo){
        ...
    }

    deleteTodo(id){
       ...
    }
}

Answer №1

It seems like there is an issue with the format of the value being passed to the ObjectId function.

_id: mongojs.ObjectId(req.params.id)

The correct format for the id should look something like '58eda0909b079aff3acb9b67'. To ensure that the value is being correctly received by the API, you may want to include a console.log(req.params.id) in your code.

Answer №2

While working on todos.js, have you made sure to properly format the request object for parsing as req.body using a middleware like router.use(bodyParser.json());, as outlined in this resource?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the cause of this situation?

Recently, I started working on a MERN app using Express. While creating the server with Express, I encountered an error. const express = require("express"); const app = express(); const PATH = require("path"); const LAYOUT = require(&qu ...

What is the best method for having Grunt monitor Compass changes without interrupting an Express server?

I have a good grasp on how to set up grunt to watch for changes in sass files and compile them, as well as starting an express server. However, I'm struggling to find a way to keep the express server running while also watching for changes in the sass ...

Is there a way for me to monitor the progress of a node.js request?

I have a node.js app that creates an image, let's call it "Image A", and then uses this "Image A" to create another image, which we'll refer to as "Composition A". When the server receives 4 image requests almost simultaneously for Composition A, ...

Navigating horizontally to find a particular element

I developed a unique Angular component resembling a tree structure. The design includes multiple branches and nodes in alternating colors, with the selected node marked by a blue dot. https://i.stack.imgur.com/fChWu.png Key features to note: The tree&ap ...

Update the TemplateUrl according to the URL Parameters (GET)

I've created a basic Angular code snippet (test.component.ts) that retrieves a variable from GET parameters: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ select ...

Sending an HTML form data to a Node.js server

I'm currently working on a simple project that involves creating a web form and sending it to node.js in order to save the information received. I've been following some YouTube tutorials (https://www.youtube.com/watch?v=rin7gb9kdpk), but I' ...

What is the process for writing code to conduct unit testing on headers using mocha and chai in a Node.js express framework application?

Currently, I am in the process of creating test cases to verify if all headers received are spelled correctly. In this scenario, there are several field names that require test cases to ensure they are accurately spelled as specified in an array of objec ...

Error: The AWS Lambda handler is throwing a TypeError, indicating that the callback must be a function

I clicked on the link: How Can I create an AWS Lambda Script for Running a Protractor / Selenium Browser Automation Script? Instead of implementing my own code, I used the following code snippet in the handler section based on the provided answer: 'us ...

What is the best way to securely store a JWT Token received from Cognito after logging in through the Cognito Hosted UI?

Our current architecture involves front end Angular and backend nodejs/express. This setup functions in the following order: User logs in to the site via Cognito Hosted UI Upon successful login, the user is redirected to our home page with a code sent in ...

js-beautify: there was an error running the command in node.js

Can anyone help me with why I am having trouble running js-beautify? I installed it but it still doesn't work properly. ...

Adding an unnecessary identification number without justification

Here is the code snippet I am working with: const Message = connection.define("message", { room: { type: sequelize.STRING, allowNull: false, }, name: { type: sequelize.STRING, defaultValue: "Anonym ...

Combining two streams in RxJS and terminating the merged stream when a particular input is triggered

I am currently developing an Angular application and working on implementing a system where an NGRX effect will make requests to a service. This service will essentially perform two tasks: Firstly, it will check the local cache (sqlite) for the requested ...

Enhancing Real-time Communication with NodeJS and NowJs Servers

Recently, I stumbled upon NodeJS and NowJS and I'm fascinated by the technology. My goal is to develop a Facebook-style instant commenting application where users can post comments that will instantly appear on the feed. After watching the screencast ...

Managing POST request data in Express: A step-by-step guide

Currently, I am facing an issue with my alert button on the client side, which has an event listener that is supposed to send data to the server. Below is the code snippet for the client side: alertBtn.addEventListener("click", () => { axios ...

I recently updated my node to version 0.8 and encountered a new issue with the sys/util functionality

After upgrading from node v0.6.12 to 0.80, I encountered the following error message - I have removed the sys module from the import but am still facing this issue. Any advice or suggestions would be greatly appreciated. Also, it's worth noting that I ...

Issue with Firebase not updating to Node version 12

I have followed the instructions provided at this link: https://firebase.google.com/docs/functions/manage-functions#upgrade-node10 As per the guidelines, I updated my package.json to specify Node 12 as the engine: "engines": { "node&quo ...

How to use Express Validator to validate both email and username within a single field?

I am currently developing an application using the Express (Node.js framework) and I want to allow users to log in with either their email address or username. My question is, how can I implement validation for both types of input on the same field using e ...

When using Javascript, you can expect to receive a modified structure that is different from

I am dealing with an array of objects that have the following structure: const data: [ { id: 21786162, label: "cBTC 2021-06-25 Put $50,000.00", active": true, type: "put", strike_price: 5000000, date_live: "2019-11- ...

The HTML element cannot be set within page.evaluate in Node.js Puppeteer

I've run into an issue while using node.js puppeteer, specifically with the page.evaluate method. I'm experiencing difficulties with this part of my code: console.log(response); //This line is valid as it prints a regular string awa ...

Increase the size of the angular material accordion

I am dealing with a situation where I have 2 different accordion components on the same page. Each accordion contains multiple expansion panels. My challenge is to close all expanded panels from both accordions and only open the currently selected expans ...