ошибка - именно то, о чем она говорит, если вы пытаетесь прочитать path
из undefined
, вы можете думать так:
const testObj = {path: 'my path'}
const path = testObj.path
это будет работать правильно? потому что все определено, но что произойдет, если вы попробуете это:
let testObj; //you only defined the name of the variable, but it is undefined.
let path = testObj.path // can not read path from undefined.
, поэтому в основном вам нужно сделать следующее:
- убедиться, что переменная определена
- установить значение по умолчанию для переменной.
убедиться, что оно определено:
app.post("/ashanti",upload.single("pic"),function(req,res){
// if some of these entries are undefined, we return
if(!req || !req.file || !req.file.path) return;
const pic=req.file.path;
....
....
....
}
установить значение по умолчанию:
app.post("/ashanti",upload.single("pic"),function(req,res){
const { path: pic }= req.file || {}; // we are sure that we are reading from an object.
if(!pic) return;
....
....
....
}