【问题标题】:What does it mean by this async Javascript server app error?这个异步 Javascript 服务器应用程序错误是什么意思?
【发布时间】:2021-06-23 23:10:10
【问题描述】:

我正在设置一个使用服务器连接到另一个站点的天气应用程序网站,并且使用了异步 javascript,但在尝试运行代码后,最后一个错误读取“未捕获的语法错误:输入意外结束”应用程序文件中的行...我不明白这是什么意思,因此我不知道如何解决它

这是我的应用文件代码

/* Global Variables */
const apiKey = "726f360f99f8ed5ce834f19b2f632fd3"
// Create a new date instance dynamically with JS
let d = new Date();
let newDate = +d.getMonth()+1+'.'+ d.getDate()+'.'+ d.getFullYear();
const gen = document.querySelector("#generate");
gen.addEventListener("click", async() =>{
    const Zcode = document.querySelector("#zip").value;
    const feel = document.querySelector("#feelings").value;
    try {
    getTemp()
    .then(temp =>{
        const object = {
            date: newDate,
            temp: temp,
        }
        return DealingWithServer()
    })
    .then(data =>{
        UpdateSite(data)
    })
    }catch(error){
        console.log(error);
    }
});
async function getTemp (){
    const res = await fetch (`https://api.openweathermap.org/data/2.5/weather=?zip=${zipCode}&appid=${apiKey}&units=metric`);
    const data= await res.json;
    const temp = data.main.temp
    return temp
}
async function DealingWithServer (){
    await fetch('/recieve', {
        method: "POST",
        credentials: "same-origin",
        headers: {"Content-Type": "application/json"},
        body:JSON.stringfy({
            date: newDate,
            temp: temp,
            feel: feel
        })
    });
    const Sres = await fetch('/get', {credentials: "same-origin"});
    const Sdata = await Sres.json()
    return (Sdata);
}
function UpdateSite (data)

和我的服务器文件代码

// Setup empty JS object to act as endpoint for all routes
projectData = {};
const port = 3000;
// Require Express to run server and routes
const express= require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
// Start up an instance of app
const app=express()
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Cors for cross origin allowance
app.use(cors())
// Initialize the main project folder
app.use(express.static('website'));
app.get("/get",(req, res) => {
    res.send(projectData)
})
app.post("/recieve", (req, res) => {
    projectData =req.body
    res.status(200)
})
// Setup Server
app.listen(3000,() =>{
    console.log("Server running");
})

【问题讨论】:

  • 如果function UpdateSite (data) 是文件的结尾,那是你的问题
  • 您缺少 UpdateSite 函数的主体。

标签: javascript node.js asynchronous server


【解决方案1】:

在我看来,您还没有完成底部的UpdateSite 函数。它应该是这样的:

function UpdateSite (data) {
  // Do things to update the site
}

由于没有函数定义,输入(本例中为 JavaScript 代码)意外结束 - 即解析器不希望输入以 function UpdateSite (data) 结束

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    相关资源
    最近更新 更多