2017-04-12 13 views
0

MongoDBをローカルにインストールしてMEANスタックにtodosアプリを構築しようとしています。 PUTリクエストで次のエラーが発生しています。どんな助けもありがとう。MEANアプリでPUTリクエストにエラーが発生しました

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) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at /mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:281:22 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:354:14) 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:365:14) 
ReferenceError: updObj is not defined 
    at module.exports (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:73:8) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at /mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:281:22 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:354:14) 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:365:14) 
    at Function.process_params (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:410:3) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:275:10) 
ReferenceError: updObj is not defined 
    at module.exports (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:73:8) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at /mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:281:22 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:354:14) 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:365:14) 
    at Function.process_params (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:410:3) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:275:10) 
ReferenceError: updObj is not defined 
    at module.exports (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:73:8) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at /mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:281:22 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:354:14) 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:365:14) 
    at Function.process_params (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:410:3) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:275:10) 
ReferenceError: updObj is not defined 
    at module.exports (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/routes/todos.js:73:8) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/layer.js:95:5) 
    at /mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:281:22 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:354:14) 
    at param (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:365:14) 
    at Function.process_params (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:410:3) 
    at next (/mnt/c/JAVADEV/projects/Servers/NodeJsV4.5Server/webapps/ToDo/node_modules/express/lib/router/index.js:275:10) 

todos.js(サーバ側)

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

router.get('/todos', function (req, res, next) { 
    db.todos.find(function (err, todos) { 
     if (err) { 
      res.send(err); 
     } else { 
      res.json(todos); 
     } 
    }); 
}); 

router.get('/todo/:id', function (req, res, next) { 
    db.todos.findOne({ _id: mongojs.ObjectId(req.params.id) }, function (err, todo) { 
     if (err) { 
      res.send(err); 
     } else { 
      res.json(todo); 
     } 
    }); 
}); 

router.post('/todo', function (req, res, next) { 
    var todo = req.body; 
    if (!todo.text || !(todo.isCompleted + '')) { 
     res.status(400); 
     res.json({ "error": "Invalid Data" }); 
    } else { 
     db.todos.save(todo, function (err, result) { 
      if (err) { 
       res.send(err); 
      } else { 
       res.json(result); 
      } 
     }); 
    } 
}); 

router.put('/todo/:id', function (req, res, next) { 
    var todo = req.body; 
    var updObj = {}; 

    if (todo.isCompleted) { 
     updObj.isCompleted = todo.isCompleted; 
    } 

    if (updObj.text) { 
     updObj.text = todo.text; 
    } 

    if (!updObj) { 
     res.status(400); 
     res.json({ "error": "Invalid Data" 
    }); 
    } else { 
     db.todos.update({ 
      _id: mongojs.ObjectId(req.params.id) 
     }, updObj, {}, function (err, result) { 
      if (err) { 
       res.send(err); 
      } else { 
       res.json(result); 
      } 
     }); 
    } 
}); 

router.delete('/todo/:id', function (req, res, next) { 
    db.todos.update({ 
     _id: mongojs.ObjectId(req.params.id) 
    }, updObj, {}, function (err, result) { 
     if (err) { 
      res.send(err); 
     } else { 
      res.json(result); 
     } 
    }); 
}); 

module.exports = router; 

todos.component.html

<div class="add-todo-form text-center"> 
    <h1>Add Todo</h1> 
    <div class="form-group"> 
     <input class="form-control input-lg" placeholder="Add Todo..." autofocus #todoText> 
     <br> 
     <button (click)="addTodo($event, todoText)" class="btn btn-primary btn-block">Create</button> 
    </div> 
</div> 

<div class="todo-list"> 
    <div *ngFor="let todo of todos"> 
     <div class="col-md-1"> 
      <input type="checkbox" [checked]="todo.isCompleted" (click)="updateStatus(todo)"> 
     </div> 
     <div class="col-md-7"> 
      <span [class.hidden]="todo.isEditMode">{{todo.text}}</span> 
      <input type="text" [class.hidden]="!todo.isEditMode" [value]="todo.text" (keypress)="updateTodo"> 
      <input type="button" [class.hidden]="!todo.isEditMode" value="Cancel" (click)="setEditState(todo)"> 
     </div> 
     <div class="col-md-4 btns"> 
      <input (click)="deleteTodo(todo)" type="button" class="btn btn-danger pull-right" value="Delete"> 
      <input [class.disabled]="todo.isCompleted" (click)="setEditState(todo, true)" type="button" class="btn btn-default pull-right" value="Edit"> 
     </div> 
    </div> 
</div> 

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', 
}) 

export class TodosComponent implements OnInit { 
    todos: Todo[]; 

    constructor(private _todoService: TodoService){ 

    } 

    ngOnInit(){ 
     this.todos = []; 
     this._todoService.getTodos() 
     .subscribe(todos => { 
      this.todos = todos; 
     }); 
    } 

    addTodo(event, todoText){ 
     var result; 
     var newTodo = { 
      text: todoText.value, 
      isCompleted: false 
     }; 

    result = this._todoService.saveTodo(newTodo); 
    result.subscribe(x => { 
     this.todos.push(newTodo); 
     todoText.value = ''; 
    }); 
    } 

    setEditState(todo, state){ 
     if(state){ 
      todo.isEditMode = state; 
     } else { 
      delete todo.isEditMode; 
     } 
    } 

    updateStatus(todo){ 
     var _todo = { 
      _id: todo._id, 
      text: todo.text, 
      isCompleted: !todo.isCompleted 
     }; 

     this._todoService.updateTodo(_todo) 
     .subscribe(data => { 
      todo.isCompleted = !todo.isCompleted; 
     }); 
    } 

    updateTodoText(event, todo){ 
     if(event.which === 13){ 
      todo.text = event.target.value; 
      var _todo = { 
      _id: todo._id, 
      text: todo.text, 
      isCompleted: todo.isCompleted 
     }; 

     this._todoService.updateTodo(_todo) 
     .subscribe(data => { 
      this.setEditState(todo, false); 
     }) 
     } 
    } 

    deleteTodo(todo){ 
     var todos = this.todos; 

     this._todoService.deleteTodo(todo._id) 
     .subscribe(data => { 
      if(data.n == 1){ 
       for(var i = 0; i < todos.length; i++){ 
        if(todos[i]._id == todo._id){ 
         todos.splice(i, 1); 
        } 
       } 
      } 
     }) 
    } 
} 

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() { 
     return this._http.get('/api/todos') 
      .map(res => res.json()); 
    } 

    saveTodo(todo){ 
     var headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 
     return this._http.post('/api/todo', JSON.stringify(todo), {headers: headers}) 
       .map(res => res.json()); 
    } 

    updateTodo(todo){ 
     var headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 
     return this._http.put('/api/todo/'+todo._id, JSON.stringify(todo), {headers: headers}) 
       .map(res => res.json()); 
    } 

    deleteTodo(id){ 
     return this._http.delete('/api/todo/'+id) 
      .map(res => res.json()); 
    } 
} 
+0

todos.jsに 'var updObj = {};'の後に 'if(updObj.text)'がありました。 –

+0

もう少し説明していただけますか? btw、if(updObj.text)をif(todo.text)に変更しました。それでも同じエラーです。 – JoeZ

+0

あなたは正しいと推測しました。代わりに 'if(todo.text)'をチェックするように思い出させました。それが 'undefined'を与えたという事実は、' req.body'がおそらく未定義であることを意味していました。 bodyParserを設定していない可能性がありますか? –

答えて

0

ObjectId関数に渡される値が有効な形式ではありません。

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

idは、 '58eda0909b079aff3acb9b67'のようにフォーマットする必要があります。値がapiに正しく来ることを検証するためにconsole.log(req.params.id)を使いたいかもしれません。

0

todos.jsでは、router.use(bodyParser.json());this linkなどのミドルウェアによってreq.bodyとして解析されるリクエストオブジェクトを準備しましたか?

関連する問題