跪求大神幫忙解決TypeError: Cannot read property 'save' of undefined
折騰了一個晚上,還是搞不定,body-parser已經(jīng)安裝了。mongodb也安裝好了,提交數(shù)據(jù)就出錯。其他功能正常。
(node:10084) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
{ CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Movie"
? ? at MongooseError.CastError (E:\webapp\imooc\node_modules\mongoose\lib\error\cast.js:26:11)
? ? at ObjectId.cast (E:\webapp\imooc\node_modules\mongoose\lib\schema\objectid.js:147:13)
? ? at ObjectId.castForQuery (E:\webapp\imooc\node_modules\mongoose\lib\schema\objectid.js:187:15)
? ? at cast (E:\webapp\imooc\node_modules\mongoose\lib\cast.js:229:32)
? ? at Query.cast (E:\webapp\imooc\node_modules\mongoose\lib\query.js:2752:12)
? ? at Query.findOne (E:\webapp\imooc\node_modules\mongoose\lib\query.js:1353:10)
? ? at E:\webapp\imooc\node_modules\mongoose\lib\query.js:2310:21
? ? at new Promise.ES6 (E:\webapp\imooc\node_modules\mongoose\lib\promise.js:45:3)
? ? at Query.exec (E:\webapp\imooc\node_modules\mongoose\lib\query.js:2304:17)
? ? at Function.findById (E:\webapp\imooc\schemas\movie.js:43:5)
? ? at E:\webapp\imooc\app.js:75:9
? ? at Layer.handle [as handle_request] (E:\webapp\imooc\node_modules\express\lib\router\layer.js:95:5)
? ? at next (E:\webapp\imooc\node_modules\express\lib\router\route.js:131:13)
? ? at Route.dispatch (E:\webapp\imooc\node_modules\express\lib\router\route.js:112:3)
? ? at Layer.handle [as handle_request] (E:\webapp\imooc\node_modules\express\lib\router\layer.js:95:5)
? ? at E:\webapp\imooc\node_modules\express\lib\router\index.js:277:22
? message: 'Cast to ObjectId failed for value "undefined" at path "_id" for model "Movie"',
? name: 'CastError',
? stringValue: '"undefined"',
? kind: 'ObjectId',
? value: 'undefined',
? path: '_id',
? reason: undefined,
? model:
? ?{ [Function: model]
? ? ?hooks: Kareem { _pres: {}, _posts: {} },
? ? ?base:
? ? ? Mongoose {
? ? ? ? connections: [Object],
? ? ? ? plugins: [],
? ? ? ? models: [Object],
? ? ? ? modelSchemas: [Object],
? ? ? ? options: [Object] },
? ? ?modelName: 'Movie',
? ? ?model: [Function: model],
? ? ?db:
? ? ? NativeConnection {
? ? ? ? base: [Object],
? ? ? ? collections: [Object],
? ? ? ? models: [Object],
? ? ? ? config: [Object],
? ? ? ? replica: false,
TypeError: Cannot read property 'save' of undefined
? ? at Movie.findById (E:\webapp\imooc\app.js:81:10)
? ? at E:\webapp\imooc\node_modules\mongoose\lib\query.js:2326:9
? ? at newTickHandler (E:\webapp\imooc\node_modules\mpromise\lib\promise.js:234:18)
? ? at _combinedTickCallback (internal/process/next_tick.js:67:7)
? ? at process._tickCallback (internal/process/next_tick.js:98:9)
2017-01-17
原因在這里
這里的ID是從表單中讀取的 沒有輸入的話 是字符串的'undefined' 并非js中未賦值的變量值的關(guān)鍵字`undefined`。
是從http://cnodejs.org/topic/552a3a7ecd5ee87201ab1edc?得到的啟示 要求ID為Objectid,是因為id不符合導(dǎo)致的錯誤,檢查代碼發(fā)現(xiàn)undefined 未加引號。
表單數(shù)據(jù)在提交時 如下所示:
2022-03-25
這個想要弄得清楚,最好學(xué)一下后臺語言,如php了解表單數(shù)據(jù)是怎么提交給服務(wù)器端的。就能完全明白。步驟都一樣,討論區(qū)有集成環(huán)境下載地址
2017-06-20
if (id !== 'undefined' && id !== '')
應(yīng)該要這樣
2017-03-28
TypeError: Cannot read property 'save' of undefined
??? at E:\stq\stq\app.js:65:11
??? at E:\stq\stq\node_modules\mongoose\lib\query.js:2745:9
??? at tryCatcher (E:\stq\stq\node_modules\bluebird\js\release\util.js:16:23)
??? at Promise._settlePromiseFromHandler (E:\stq\stq\node_modules\bluebird\js\release
\promise.js:512:31)
??? at Promise._settlePromise (E:\stq\stq\node_modules\bluebird\js\release\promise.js
:569:18)
??? at Promise._settlePromise0 (E:\stq\stq\node_modules\bluebird\js\release\promise.j
s:614:10)
??? at Promise._settlePromises (E:\stq\stq\node_modules\bluebird\js\release\promise.j
s:689:18)
??? at Async._drainQueue (E:\stq\stq\node_modules\bluebird\js\release\async.js:133:16
)
??? at Async._drainQueues (E:\stq\stq\node_modules\bluebird\js\release\async.js:143:1
0)
??? at Immediate.Async.drainQueues (E:\stq\stq\node_modules\bluebird\js\release\async
.js:17:14)
??? at runCallback (timers.js:651:20)
??? at tryOnImmediate (timers.js:624:5)
??? at processImmediate [as _immediateCallback] (timers.js:596:5)
2017-03-28
為啥我有單引號也不行
2017-01-17
同求 相同的問題
2017-01-15
問題出在這段代碼里,找了半天,一句一句跟人家對,還是找不出毛病。。。懇請各位大哥大姐幫我看看問題在哪。。。
2017-01-15
這是app.js 源碼