簡體   English   中英

類型錯誤:urls.toObject 不是 function Mongoose

[英]TypeError: urls.toObject is not a function Mongoose

Hi everyone I'm trying to do a basic to object function in my code I required mongoose and its send me this error I don't understand why: UnhandledPromiseRejectionWarning: TypeError: urls.toObject is not a function. 它在get function中,希望得到幫助。

 const express = require("express"); const { v4: uuidv4 } = require("uuid"); const Url = require("../models/Url"); const mongoose = require("mongoose"); const HttpError = require("../models/http-errors"); const getTodo = async (req, res, next) => { let urls; try { urls = await Url.find({}); res.json({ urls }); } catch (err) { const error = new HttpError( "Something went wrong, could not find a Url.", 500 ); return next(error); } res.json({ urls: urls.toObject({ getters: true }) }); }; const addTodo = async (req, res, next) => { const { content } = req.body; const createdPlace = new Url({ content: content, enable: false, }); // Sending it To Mongo try { await createdPlace.save(); } catch (err) { const error = new HttpError("Creating Place fails mongo"); return next(error); } res.sendStatus(200); }; exports.getTodo = getTodo; exports.addTodo = addTodo;

URL 是 object 的列表,所以我需要 map 來做一個 toObject ZC1C42145268E68394745

 res.json({ url: url.map((url) => url.toObject({ getters: true })), });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM