I'm trying to update an entry in my mongodb but somehow it fails. I get no error message, is not sucessfull the put does not seem to be successful. The network message shows no error code but also no header.I would really appreciate it, if someone could help me.Server:const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const app = express(); // Middleware app.use(bodyParser.json()); app.use(cors()); const sentences = require('./routes/api/sentences'); app.use('/api/sentences', sentences); // Handle production if (process.env.NODE_ENV === 'production') { // Static folder app.use(express.static(__dirname + '/public/')); // Handle SPA app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html')); } const port = process.env.PORT || 5000; app.listen(port, () => console.log(`Server started on port ${port}`)); api:const express = require('express'); const mongodb = require('mongodb'); const router = express.Router(); // Get sentences router.get('/', async (req, res) => { const sentences = await loadSentencesCollection(); res.send(await sentences.find({}).toArray()); }); var Sentences = require("../../models/sentences"); // update sentence router.put('/:id', (req, res) => { Sentences.findByIdAndUpdate( req.params.id, { $set: {"annotations": req.body.annotations}}, function(err, result){ if(err){ return res.status(500).json(err); } console.log("RESULT: " + result); res.send('Done') }); }) async function loadSentencesCollection() { const client = await mongodb.MongoClient.connect( 'mongodb://localhost:27017/', { useNewUrlParser: true } ); return client.db('annotationDB').collection('sentences'); } module.exports = router; Model:var mongoose = require("mongoose"); var Schema = mongoose.Schema; var SentencesSchema = new Schema({ sent: String, annotations: Object }); var Sentences = mongoose.model("sentences", SentencesSchema); module.exports = Sentences; DataService:import axios from 'axios'; const url = 'http://localhost:5000/api/sentences/'; class DataService { static getSentences(){ return new Promise(async (resolve,reject)=>{ try{ const res = await axios.get(url); const data = res.data; resolve( data.map(sentence => ({ ...sentence, id: new String(sentence._id), sent: new String(sentence.sent), annotation: new Object(sentence.annotations) })) ); } catch(err){ reject(err); } }) } static updateSentence (params) { return axios.put(url + params.id, params) } } export default DataService; Updatefunction in vue:async updateSentence () { await DataService.updateSentence({ id: this.sentences[this.ids[0]].id, sent: this.sentences[this.ids[0]].sent, annotations: this.sentences[this.ids[0]].annotations }) this.$router.push({ name: 'Sentences' }) }, I logged the querry:Query { _mongooseOptions: {}, _transforms: [], _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} }, _executionCount: 0, mongooseCollection: NativeCollection { collection: null, Promise: [Function: Promise], opts: { bufferCommands: true, capped: false, Promise: [Function: Promise], '$wasForceClosed': undefined }, name: 'sentences', collectionName: 'sentences', conn: NativeConnection { base: [Mongoose], collections: [Object], models: [Object], config: [Object], replica: false, options: null, otherDbs: [], relatedDbs: {}, states: [Object: null prototype], _readyState: 0, _closeCalled: false, _hasOpened: false, plugins: [], _listening: false }, queue: [], buffer: true, emitter: EventEmitter { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, [Symbol(kCapture)]: false } }, model: Model { sentences }, schema: Schema { obj: { sent: [Function: String], annotations: [Function: Object] }, paths: { sent: [SchemaString], annotations: [Mixed], _id: [ObjectId], __v: [SchemaNumber] }, aliases: {}, subpaths: {}, virtuals: { id: [VirtualType] }, singleNestedPaths: {}, nested: {}, inherits: {}, callQueue: [], _indexes: [], methods: {}, methodOptions: {}, statics: {}, tree: { sent: [Function: String], annotations: [Function: Object], _id: [Object], __v: [Function: Number], id: [VirtualType] }, query: {}, childSchemas: [], plugins: [ [Object], [Object], [Object], [Object], [Object] ], '$id': 1, s: { hooks: [Kareem] }, _userProvidedOptions: {}, options: { typePojoToMixed: true, typeKey: 'type', id: true, noVirtualId: false, _id: true, noId: false, validateBeforeSave: true, read: null, shardKey: null, autoIndex: null, minimize: true, discriminatorKey: '__t', versionKey: '__v', capped: false, bufferCommands: true, strict: true, pluralization: true }, '$globalPluginsApplied': true }, op: 'findOneAndUpdate', options: {}, _conditions: { _id: '5e2e158f7aa2ac538a005281' }, _fields: undefined, _update: { '$set': { annotations: [Object] } }, _path: undefined, _distinct: undefined, _collection: NodeCollection { collection: NativeCollection { collection: null, Promise: [Function: Promise], opts: [Object], name: 'sentences', collectionName: 'sentences', conn: [NativeConnection], queue: [], buffer: true, emitter: [EventEmitter] }, collectionName: 'sentences' }, _traceFunction: undefined, '$useProjection': true }
Submitted January 29, 2020 at 02:30PM by TheFrankBaconian
No comments:
Post a Comment