Deploying Firebase Typescript Function Error; unmarshalling package.json: json: cannot unmarshal bool into Go struct field PackageJSON.devDependencies

huangapple go评论71阅读模式
英文:

Deploying Firebase Typescript Function Error; unmarshalling package.json: json: cannot unmarshal bool into Go struct field PackageJSON.devDependencies

问题

以下是您提供的代码的翻译:

我试图部署一个云函数到Firebase但我得到以下错误消息

构建失败解封包 package.json 时出错json无法将布尔值解封为 Go 结构字段 PackageJSON.devDependencies 类型的字符串错误 ID9f520bcf

这是我的 index.ts 文件
import * as functions from 'firebase-functions';
import retrievePositionData from './retrievePositionData';

const admin = require('firebase-admin');
admin.initializeApp();

export const fetchLocations = functions.pubsub
  .schedule('*/10 * * * *')
  .timeZone('America/Montreal')
  .onRun(retrievePositionData);

这是我的自定义函数,用于获取 JSON 并写入 Firestore 的 retrievePositionData.ts 文件:

import axios from "axios";
import { firestore } from 'firebase-admin';
import { Client, ElevationResponse } from "@googlemaps/google-maps-services-js";
import { config } from 'firebase-functions';

type SpotResponse = {
  response: {
    feedMessageResponse: {
      count: number,
      messages: {
        message: Array<{
          latitude: number,
          longitude: number,
          unixTime: number,
          messageType: string,
          messageContent: string,
          batteryState: string,
        }>,
      },
    },
    errors: Array<{}>,
  },
}

export default async () => {
  const client = new Client({});
  const FirestoreInstance = firestore();
  const feedID = '0u88e3hrgRoCDA4JEqkg41U1jhO25PbvW';
  let offset = 0;
  while (true) {
    const url = `https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/${feedID}/message.json?start=${offset}`;
    const response = await axios.get<SpotResponse>(url)
    if (response.data.response.errors) {
      console.log('已达到反馈的末尾。停止。');
      return;
    }
    const messages = response.data.response.feedMessageResponse.messages.message;

    while (messages.length > 0) {
      const message = messages.shift();
      if (!message) throw new Error('队列中没有更多项目');

      const query = await FirestoreInstance.collection('locationHistory').where('timestamp', '==', firestore.Timestamp.fromMillis(message.unixTime * 1000)).limit(1).get();
      if(!query.empty) {
        console.log('找到重复条目。停止。');
        return;
      }

      const elevationResponse: ElevationResponse = await client.elevation({
        params: {
          locations: [{ lat: message.latitude, lng: message.longitude }],
          key: config().maps.key,
        },
        timeout: 1000,
      });
      
      const formatedMessage: any = {
        timestamp: firestore.Timestamp.fromMillis(message.unixTime * 1000),
        location: new firestore.GeoPoint(message.latitude, message.longitude),
        messageType: message.messageType,
        messageContent: message.messageContent,
        batteryState: message.batteryState,
        elevation: elevationResponse.data.results[0].elevation,
      }
      Object.keys(formatedMessage).forEach(key => formatedMessage[key] === undefined && delete formatedMessage[key]);

      FirestoreInstance.collection('locationHistory').doc().create(formatedMessage)
        .then((value: firestore.WriteResult) => {
          console.log('成功插入文档', formatedMessage, '于', value.writeTime);
        })
        .catch((reason: any) => {
          console.error('无法插入文档', reason);
        });
    }

    offset += response.data.response.feedMessageResponse.count;
  }
}

这是 package.json 文件:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@googlemaps/google-maps-services-js": "^3.1.4",
    "axios": "^0.19.2",
    "express": "^4.17.1",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^4.9.4",
    "firebase-functions-test": "^0.2.0",
    "private": true
  }
}

希望这对您有所帮助!如果您需要更多翻译,请告诉我。

英文:

I'm trying to deploy a cloud function to firebase and I'm getting the following error message.

Build failed: unmarshalling package.json: json: cannot unmarshal bool into Go struct field PackageJSON.devDependencies of type string; Error ID: 9f520bcf

This is my index.ts

import * as functions from &#39;firebase-functions&#39;;
import retrievePositionData from &#39;./retrievePositionData&#39;;
const admin = require(&#39;firebase-admin&#39;);
admin.initializeApp();
export const fetchLocations = functions.pubsub
.schedule(&#39;*/10 * * * *&#39;)
.timeZone(&#39;America/Montreal&#39;)
.onRun(retrievePositionData);

This is my custom function pulling JSON to write to Firestore retrievePositionData.ts.

import axios from &quot;axios&quot;;
import { firestore } from &#39;firebase-admin&#39;;
import { Client, ElevationResponse } from &quot;@googlemaps/google-maps-services-js&quot;;
import { config } from &#39;firebase-functions&#39;;
type SpotResponse = {
response: {
feedMessageResponse: {
count: number,
messages: {
message: Array&lt;{
latitude: number,
longitude: number,
unixTime: number,
messageType: string,
messageContent: string,
batteryState: string,
}&gt;,
},
},
errors: Array&lt;{}&gt;,
},
}
export default async () =&gt; {
const client = new Client({});
const FirestoreInstance = firestore();
const feedID = &#39;0u88e3hrgRoCDA4JEqkg41U1jhO25PbvW&#39;;
let offset = 0;
while (true) {
const url = `https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/${feedID}/message.json?start=${offset}`;
const response = await axios.get&lt;SpotResponse&gt;(url)
if (response.data.response.errors) {
console.log(&#39;Reached the end of the feed. Stopping.&#39;);
return;
}
const messages = response.data.response.feedMessageResponse.messages.message;
while (messages.length &gt; 0) {
const message = messages.shift();
if (!message) throw new Error(&#39;No more items in the queue&#39;);
const query = await FirestoreInstance.collection(&#39;locationHistory&#39;).where(&#39;timestamp&#39;, &#39;==&#39;, firestore.Timestamp.fromMillis(message.unixTime * 1000)).limit(1).get();
if(!query.empty) {
console.log(&#39;Found duplicate entry. Stopping.&#39;);
return;
}
const elevationResponse: ElevationResponse = await client.elevation({
params: {
locations: [{ lat: message.latitude, lng: message.longitude }],
key: config().maps.key,
},
timeout: 1000,
});
const formatedMessage: any = {
timestamp: firestore.Timestamp.fromMillis(message.unixTime * 1000),
location: new firestore.GeoPoint(message.latitude, message.longitude),
messageType: message.messageType,
messageContent: message.messageContent,
batteryState: message.batteryState,
elevation: elevationResponse.data.results[0].elevation,
}
Object.keys(formatedMessage).forEach(key =&gt; formatedMessage[key] === undefined &amp;&amp; delete formatedMessage[key]);
FirestoreInstance.collection(&#39;locationHistory&#39;).doc().create(formatedMessage)
.then((value: firestore.WriteResult) =&gt; {
console.log(&#39;Successfully inserted document&#39;, formatedMessage, &#39;at&#39;, value.writeTime);
})
.catch((reason: any) =&gt; {
console.error(&#39;Could not insert document&#39;, reason);
});
}
offset += response.data.response.feedMessageResponse.count;
}
}

And this is package.json

{
&quot;name&quot;: &quot;functions&quot;,
&quot;scripts&quot;: {
&quot;lint&quot;: &quot;tslint --project tsconfig.json&quot;,
&quot;build&quot;: &quot;tsc&quot;,
&quot;serve&quot;: &quot;npm run build &amp;&amp; firebase emulators:start --only functions&quot;,
&quot;shell&quot;: &quot;npm run build &amp;&amp; firebase functions:shell&quot;,
&quot;start&quot;: &quot;npm run shell&quot;,
&quot;deploy&quot;: &quot;firebase deploy --only functions&quot;,
&quot;logs&quot;: &quot;firebase functions:log&quot;
},
&quot;engines&quot;: {
&quot;node&quot;: &quot;10&quot;
},
&quot;main&quot;: &quot;lib/index.js&quot;,
&quot;dependencies&quot;: {
&quot;@googlemaps/google-maps-services-js&quot;: &quot;^3.1.4&quot;,
&quot;axios&quot;: &quot;^0.19.2&quot;,
&quot;express&quot;: &quot;^4.17.1&quot;,
&quot;firebase-admin&quot;: &quot;^8.10.0&quot;,
&quot;firebase-functions&quot;: &quot;^3.6.1&quot;
},
&quot;devDependencies&quot;: {
&quot;tslint&quot;: &quot;^5.12.0&quot;,
&quot;typescript&quot;: &quot;^4.9.4&quot;,
&quot;firebase-functions-test&quot;: &quot;^0.2.0&quot;,
&quot;private&quot;: true
}
}

答案1

得分: 0

Remove this line from package.json. It's not a devDependency, yet you have it nested there:

"private": true
英文:

Remove this line from package.json. It's not a devDependency, yet you have it nested there:

    &quot;private&quot;: true

huangapple
  • 本文由 发表于 2023年5月18日 09:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定